将Haskell GHCi命令结果输出到一个txt文件 [英] Outputting Haskell GHCi command results to a txt file
本文介绍了将Haskell GHCi命令结果输出到一个txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难将GHCi的命令结果输出到文件中。我想知道如果有人能给我一个关于如何做到这一点的简单解释?我迄今在网上找到的例子似乎过于复杂。
解决方案
假设您有一个函数 mungeData
p>
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
您可以这样写入文件:
ghci> writeFilemyoutput.txt(show(mungeData [1..5])
我会倾向于写入
ghci> writeFilemyoutput.txt$ show $ mungeData [1..5]
除去一些括号。
读回来in
您可以使用
ghci> fmap( read :: String - > [Int])$ readFilemyoutput.txt
line
你可以像这样输出一行数字:
ghci> writeFilemyoutput'.txt$ unlines.map show $ mungeData [1..5]
ghci> fmap(map read.lines :: String - > [Int])$ readFilemyoutput'.txt
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.
解决方案
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"
这篇关于将Haskell GHCi命令结果输出到一个txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文