我刚刚开始学习Haskell,我在尝试弄清楚文件读取的工作方式时遇到了很多麻烦.
例如,我有一个文本文件"test.txt"它包含数字行,例如:
32 4
2 30
300 5
Run Code Online (Sandbox Code Playgroud)
我想阅读每一行,然后评估每个单词并添加它们.因此,我到目前为止尝试做这样的事情:
import System.IO
import Control.Monad
main = do
let list = []
handle <- openFile "test.txt" ReadMode
contents <- hGetContents handle
singlewords <- (words contents)
list <- f singlewords
print list
hClose handle
f :: [String] -> [Int]
f = map read
Run Code Online (Sandbox Code Playgroud)
我知道这是完全错误的,但我根本不知道如何正确使用语法.任何帮助将不胜感激.除了这个代码的示例和解释之外的好教程的链接:http: //learnyouahaskell.com/input-and-output我已经完全阅读了它