Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:48:52+00:00 2026-06-01T15:48:52+00:00

The task: I am trying to create a custom data type and have it

  • 0

The task: I am trying to create a custom data type and have it able to print to the console. I also want to be able to sort it using Haskell’s natural ordering.

The issue: Write now, I can’t get this code to compile. It throws the following error: No instance for (Show Person) arising from a use of 'print'.

What I have so far:

-- Omitted working selection-sort function

selection_sort_ord :: (Ord a) => [a] -> [a]
selection_sort_ord xs = selection_sort (<) xs

data Person = Person { 
    first_name :: String, 
    last_name :: String,   
    age :: Int }            

main :: IO ()
main = print $ print_person (Person "Paul" "Bouchon" 21)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:48:53+00:00Added an answer on June 1, 2026 at 3:48 pm

    You need a Show instance to convert the type to a printable representation (a String). The easiest way to obtain one is to add

    deriving Show
    

    to the type definition.

    data Person = Person { 
        first_name :: String, 
        last_name :: String,   
        age :: Int }
          deriving (Eq, Ord, Show)
    

    to get the most often needed instances.

    If you want a different Ord instance, as suggested in the comments, instead of deriving that (keep deriving Eq and Show unless you want different behaviour for those), provide an instance like

    instance Ord Person where
        compare p1 p2 = case compare (age p1) (age p2) of
                          EQ -> case compare (last_name p1) (last_name p2) of
                                  EQ -> compare (first_name p1) (first_name p2)
                                  other -> other
                          unequal -> unequal
    

    or use pattern matching in the definition of compare if you prefer,

        compare (Person first1 last1 age1) (Person first2 last2 age2) =
            case compare age1 age2 of
              EQ -> case compare last1 last2 of
                      EQ -> compare first1 first2
                      other -> other
              unequal -> unequal
    

    That compares according to age first, then last name, and finally, if needed, first name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom search but getting stuck. What I want is
I am trying to create a task of data import, my case is :
I am trying to figure out how to accomplish the following task: Create a
I have a very simple task I am trying to do in Groovy but
I'm trying to write an MSBuild task to build a database using FluentNhibernate mappings.
I'm trying to do a simple task with jQuery: I have a list of
I'm trying to create a scheduled task (in WinXP) which runs every 10 minutes,
I am trying to create a Task schema in my database. One field is
I'm trying to create a simple app, whose main task is to open the
I'm currently trying to build a custom connection manager and custom data flow source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.