過去幾天我一直在試圖學習Haskell。雖然我正在慢慢變好,但我發現很難用Haskell的IO進行推理,可能是由於我缺乏知識。我一直在努力編寫一個簡單的待辦事項列表程序。下面是我得到了什麼:Haskell類型錯誤消息
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是mtodo :: [IO String] -> [IO String] -> [IO String]
和TADD,TDEL,TLS是:: [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 }
任何想法有什麼錯我的類型? (另外 - 有什麼我應該改變?)。由於
回報是你的朋友 – zurgl 2013-02-14 15:11:07
明確鍵入東西!當你感到困惑時,它會讓你更容易推理,因爲它會迫使你問自己這樣的問題,例如*我想讓它返回哪種類型?*和*這是什麼東西?*。 – gspr 2013-02-14 15:13:03
順便說一下,通常最好使用空格代替Haskell代碼的製表符,而不僅僅是因爲它適用於複製粘貼到SO問題。 :] Haskell關心對齊,而不僅僅是縮進,並且標籤的解釋方式可能與您的編輯器顯示它們的方式不匹配。 – 2013-02-14 15:37:35