0

I am trying to write a list into a file and later on I want to read the file contents into the list as well.

So I have a list like this ["ABC","DEF"]

I have tried things like

hPrint fileHandle listName

This just prints into file "["ABC","DEF"]"

I have tried unlines but that is priniting like "ABC\nDEF\n"

Now in both the cases, I cant read back into proper list. The output file has quotes and because of which when I read, I get like this ["["ABC","DEF"]""] i.e a single string in list.

As I am not succeeding in this, I tried to write the list line by line, I tried to apply a map and the function to write the list k = map (\x -> hPrint fileSLC x) fieldsBefore, it is not doing anything, file is blank. I think if I write everything in separate line, I will be able to read like (lines src) later on.

I know whatever I am doing is wrong but I am writing the code on Haskell for second time only, last time I just a wrote a very a small file reading program. Moving from imperative to functional is not that easy. :(

6
  • 1
    If you need store/restore data types you are looking for a data serialization. If you are using GHC, there is a stock package called binary which does all this stuff out of the box, including serialization to a file. Most common types are already supported, your own types can be trivially added by writing custom Data.Binary instance. Commented Aug 24, 2014 at 14:14
  • Hi David, Thanks for your reply. I can't use serailization because the files I am writing to will be evaluated with human eye, as this is a school project. So I need to keep it simple text only. On a completely different note, any idea how can I input a list from user? Thanks a lot again. :) Commented Aug 24, 2014 at 14:27
  • Just build a list from values user did entered. You may or may not parse the input, depending the way input has to be performed. Be aware basic list in haskell is homogenous. Commented Aug 24, 2014 at 14:38
  • Yes actually I do not know how I can iterate for user input and store into list. Third day with Haskell :-| Commented Aug 24, 2014 at 14:54
  • 1
    You need to learn before dive in. Try LYH . Obvious way is to use recursion instead of iteration. Just write an IO function which handles input including a way of termination. Unless entered a termination string, input function would call itself. Commented Aug 24, 2014 at 16:19

2 Answers 2

3

Try using hPutStrLn and unlines instead of hPrint. The hPrint internally calls show which causes Strings to be quoted and escaped.

hPutStr fileHandle (unlines listName)

Alternatively, use a mapM or a forM. A verbose example is:

forM_ listName $ \string ->
   hPutStrLn string

This can be simplified ("eta-contracted", in lambda-calculus terminology) to

forM_ listName hPutStrLn
Sign up to request clarification or add additional context in comments.

2 Comments

You just saved my day. Thanks a lot :) :) One more thing, any idea how I can input a list from user? Thanks again. :)
@AbhinavSingh If you want to read a list in Haskell format from stdin, you can use readLn.
0

As you have seen, when you read from a file, you get a String. In order to convert this String into a list, you will need to parse it.

For k = map (\x -> hPrint fileSLC x) fieldsBefore to work, you need to use mapM or mapM_ instead of map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.