
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 !!
Your problem is in the format of your data file. After one use of
textfile, the file contains the following:That’s a good list, and it works. After the second use of
textfile, the file contains the following:That’s not a good list, and it fails. You can see this in
ghci:It’s clear that
readexpects 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.
This is a little tricky because of two tings:
readFileis lazy, andwriteFileto 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 (theseqfunction makes sure that the length is computed before the write operation happens).catchclause is used above to handle that exceptional case.