在过去的几天里,我一直在尝试学习 Haskell。虽然我正在慢慢好转,但我发现很难用 Haskell 的 IO 推理,可能是因为我缺乏知识。我一直在尝试编写一个简单的待办事项列表程序。这是我所拥有的:
tadd todo = do
td <- getLine
td:todo
tdel todo = do
trem <- getLine
let rid = read trem :: Int
[todo !! x | x <- [0..(length todo-1)], not $ x == rid]
tls todo = do
mapM putStrLn [ (show x) ++ (todo !! x) | x <- [0..(length todo -1)] ]
todo
mtodo "add" todo = tadd todo
mtodo "del" todo = tdel todo
mtodo "ls" todo = tls todo
bege = do
com <- getLine
mtodo com []
main = bege
我排除了 mtodo to bemtodo :: [IO String] -> [IO String] -> [IO String]
和 tadd, tdel, tls to be :: [IO String] -> [IO String]
。
相反,我只是收到这个可怕的错误消息
[1 of 1] Compiling Main ( todo.hs, todo.o )
todo.hs:3:9:
Couldn't match type `[]' with `IO'
Expected type: IO String
Actual type: [String]
In a stmt of a 'do' block: td : todo
In the expression:
do { td <- getLine;
td : todo }
In an equation for `tadd':
tadd todo
= do { td <- getLine;
td : todo }
todo.hs:8:9:
Couldn't match expected type `IO' with actual type `[]'
In a stmt of a 'do' block:
[todo !! x | x <- [0 .. (length todo - 1)], not $ x == rid]
In the expression:
do { trem <- getLine;
let rid = ...;
[todo !! x | x <- [0 .. (length todo - 1)], not $ x == rid] }
In an equation for `tdel':
tdel todo
= do { trem <- getLine;
let rid = ...;
[todo !! x | x <- [0 .. (length todo - 1)], not $ x == rid] }
todo.hs:12:9:
Couldn't match type `[]' with `IO'
Expected type: IO [Char]
Actual type: [[Char]]
In a stmt of a 'do' block: todo
In the expression:
do { mapM
putStrLn [(show x) ++ (todo !! x) | x <- [0 .. (length todo - 1)]];
todo }
In an equation for `tls':
tls todo
= do { mapM
putStrLn [(show x) ++ (todo !! x) | x <- [0 .. (length todo - 1)]];
todo }
任何想法我的类型有什么问题?(另外 - 有什么我应该改变的吗?)。谢谢