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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:28+00:00 2026-05-26T18:11:28+00:00

I am very new in haskell, i wrote the code for item details and

  • 0

sample input/output

I am very new in haskell, i wrote the code for item details and search the detail for each item.

type Code = Int
type Name = String 
type Database = (Code,Name)

textfile::IO()
textfile = appendFile "base.txt" (show[(110,"B")])

code for search

fun::IO ()
fun=do putStrLn"Please enter the code of the product"
       x<-getLine
       let y = read x :: Int
       show1 y 

textshow::IO [Database]
textshow= do x<-readFile "base.txt"
             let y=read x::[Database]
         return y

show1::Code->IO ()
show1 cd= do o<-textshow
             let d=[(x,y)|(x,y)<-o,cd==x]
         putStr(show d)

but, the problem is, it is working good for single data, if i append another data, then it showing error Prelude.read: no parse when i am trying to search the item.
Help will be appreciated !!

  • 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-26T18:11:28+00:00Added an answer on May 26, 2026 at 6:11 pm

    Your problem is in the format of your data file. After one use of textfile, the file contains the following:

    [(110,"B")]
    

    That’s a good list, and it works. After the second use of textfile, the file contains the following:

    [(110,"B")][(110,"B")]
    

    That’s not a good list, and it fails. You can see this in ghci:

    *Main> read "[(110,\"B\")][(110,\"B\")]" :: [Database]
    *** Exception: Prelude.read: no parse
    

    It’s clear that read expects a single list, not two lists following each other.

    If you want to append to a file that contains a single Haskell list, you need to read the file, append to the list, and write the new list in the file as a replacement.

    addToFileList :: (Read a, Show a) => FilePath -> a -> IO ()
    addToFileList fp a = do olds <- readFile fp `catch` \e ->
                                    if isDoesNotExistError e 
                                    then return "[]"
                                    else ioError e
                            let oldl = read olds
                                newl = oldl ++ [a]
                                news = show newl
                            length news `seq`
                                   writeFile fp news
    

    This is a little tricky because of two tings:

    • readFile is lazy, and writeFile to the same file can fail unless one makes sure that the whole file has already been read. In the function above this is solved by asking for the length of the new string before writing the file (the seq function makes sure that the length is computed before the write operation happens).
    • the file may not exist, the catch clause is used above to handle that exceptional case.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am from OO background and very new to Haskell. In order to print
Sorry if the question is very elementary, I am still very new to Haskell.
I'm brand new to Haskell and have very little idea what I'm doing. I've
I am very new to Haskell. Today when I reading the book and practice
I'm very new to Haskell, and I have a simple question. What function can
I am very new to Haskell. Could someone please explain why defining a list
New to Haskell so sorry if this is very basic This example is taken
I'm very new with Haskell, only starting to learn it. I'm using Learn You
[Disclaimer] I am very new to Haskell (and any FPL for that matter), just
Please bear with me as I am very new to functional programming and Haskell.

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.