我創建了這個haskell函數來從列表中刪除奇數,並嘗試在ghci中。儘管我已啓用多行模式並使用4個空格作爲縮進,但我仍然收到以下錯誤。讓多行haskell函數工作在ghci
Prelude> :set +m
Prelude> let removeOdds nums =
Prelude| if null nums
Prelude| then []
Prelude| else
Prelude| if (mod (head nums)/2) == 0
Prelude| then (head nums) : (removeOdds(tail nums))
Prelude| else removeOdds(tail nums)
Prelude|
:11:5: 解析錯誤(可能是不正確的縮進或不匹配的括號)
我讀this頁有關公共mistable新手的會讓我改變了我的代碼如下
Prelude> let removeOdds nums =
Prelude| do if null nums
Prelude| then []
Prelude| else
Prelude| do if mod((head nums)/2) == 0
Prelude| then head nums: removeOdds(tail nums)
Prelude| else removeOdds(tail nums)
Prelude|
<interactive>:47:5:
parse error (possibly incorrect indentation or mismatched brackets)
現在我結束了一個新的錯誤。在哈斯克爾看來,縮進是一件困難的事情。
順便說一句 - 在ghci中輸入多行語句是一種痛苦。我通常使用一個編輯器窗口並行運行ghci的控制檯窗口。用'ghci program.hs'調用ghci。在編輯器中更改它後,使用':r'重新加載'program.hs'。還可以看看':e'和':set editor'命令。請參閱GHCi手冊中的[__Loading source files__](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#loading-source-files)。 – ErikR
多行函數的一個常見來源是重新發明輪子:'let removeOdds = filter even'。 – chepner