I am trying to take user input and convert it in the form of a list of tuples.
What I want to do is that, I need to take the data from the user and convert it in the form of
[(Code,Name, Price)] and finally combine this user input with the previous list and write the new list to the same file.
The problem I am facing is that as soon as the program completes taking user input, WinHugs is showing an error like this Program error: Prelude.read: no parse.
Here is the code:
type Code=Int
type Price=Int
type Name=String
type ProductDatabase=(Code,Name,Price)
finaliser=do
a<-input_taker
b<-list_returner
let w=a++b
outh <- openFile "testx.txt" WriteMode
Print outh w
Close outh
The problem is that you’re using lazy IO to read from a file while you’re writing to it at the same time. This causes problems when
readsees data that has been partially written.We need to force the reading of the input data to be complete before you try writing to the file. One way of doing this is to use
seqto force the list of products to be read into memory.Also, this will fail if the file is empty. The file should contain at least
[]before running your code the first time, so that it will parse as the empty list.