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 5936419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:23:46+00:00 2026-05-22T15:23:46+00:00

Thanks to this excellent tutorial , I know how to read a string (in

  • 0

Thanks to this excellent tutorial, I know how to read a string (in this case read from a file at people.txt directly into a type synonym:

type Person = [Int]

like this:

people_text <- readFile "people.txt"
let people :: [Person]
    people = read people_text

What I want to do is use a datatype (instead of a type synonym).

Any pointers on what I am missing here? I thought I would be able to read string-data directly into a Person – defined like this (credit to learnyouahaskell.com)

data Person = Person String String Int Float String String deriving (Show)

When I try the obvious

 txt <- readFile "t.txt" (this works OK)

with t.txt containing

"Buddy" "Finklestein" 43 184.2 "526-2928" "Chocolate"

I get this error:

No instance for (Read Person)

  • 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-05-22T15:23:47+00:00Added an answer on May 22, 2026 at 3:23 pm

    First of all, you need to derive Read for your type.

    You can think of read and show as being opposites, and a sort of poor man’s serialization. show lets you convert to String, read converts from String, and in most cases the String produced should also be valid Haskell code that, when compiled, produces the same value that read gives you.

    On that note, the contents of your file aren’t going to work, because that’s not the format used by the default implementations of read and show, i.e. the implementations you get by putting Read and Show in the deriving clause.

    For example, given this:

    data Person = Person String String Int Float String String deriving (Read, Show)
    
    buddy = Person "Buddy" "Finklestein" 43 184.2 "526-2928" "Chocolate"
    

    Then in GHCi, we get:

    > show buddy
    "Person \"Buddy\" \"Finklestein\" 43 184.2 \"526-2928\" \"Chocolate\""
    

    The quotes are escaped because that’s a String value. In the file, it would look like this:

    Person "Buddy" "Finklestein" 43 184.2 "526-2928" "Chocolate"
    

    Which you’ll note is the same as the original definition in the source file.

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

Sidebar

Related Questions

i followed this excellent as3 preloader tutorial, and below is the code i have
Thanks to this excellent analysis of the Plot algorithm by Yaroslav Bulatov, I now
I asked this question and got an excellent answer (thanks!). Part of the problem
Now that I can make useful user controls in WPF (thanks to this stackoverflow
I am using Vim for windows installed in Unix mode. Thanks to this site
Thanks for reading this. I am dynamically generating some data which includes a select
Thanks for reading this. I have markup similar to what is below. Using the
Thanks for reading this I thought I could use find(), but couldn't make it
Thanks for reading this. I would have thought it would be as simple as
I'm reading about Rails fixtures in this guide (thanks, trevorturk). It appears you define

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.