Outputting Haskell GHCi command results to a txt file

haskell

Solution

Let's suppose you have a function `mungeData` and you do

 ghci> mungeData [1..5]
 [5,2,5,2,4,6,7,4,6,78,4,7,5,3,57,7,4,67,4,6,7,4,67,4]

writeFile

You can write this to file like this:

ghci> writeFile "myoutput.txt" (show (mungeData [1..5])

I'd be inclined to write

ghci> writeFile "myoutput.txt" $ show $ mungeData [1..5]

to get rid of a few brackets.

Reading it back in

You could get that back using

ghci> fmap (read::String -> [Int]) $ readFile "myoutput.txt"

One number per line

You could output it a line per number like this:

ghci> writeFile "myoutput'.txt" $ unlines.map show $ mungeData [1..5]

which reads back in as

ghci> fmap (map read.lines::String -> [Int]) $ readFile "myoutput'.txt"

Problem

I am new to Haskell. I am having a really difficult time outputting command results from GHCi to a file. I was wondering if someone can give me a simple explanation on how to do this? The examples I have found online so far seem over complicated.

Original source

Related problems