【发布时间】:2015-10-31 22:09:20
【问题描述】:
我目前正在通过 Learn You a Haskell for Great Good 进行学习,并且我正在尝试修改第九章“输入和输出”中的一个代码 sn-ps 以正确处理错误:
main = do
(command:args) <- getArgs
let result = lookup command dispatch
if result == Nothing
then
errorExit
else
let (Just action) = result
action args
在哪里
dispatch :: [(String, [String] -> IO ())]
是一个关联列表
和
errorExit :: IO ()
是一些打印错误信息的函数。
用 GHC 编译会给出错误信息
todo.hs:20:13: parse error in let binding: missing required 'in'
这(据我所知)似乎是说这里的“让”没有意识到它在“做”块中。
在第五行和第七行(分别在“then”和“else”之后)添加“do”,将错误消息更改为
todo.hs:20:13:
The last statement in a 'do' block must be an expression
let (Just action) = result
todo.hs:21:5: Not in scope: `action'.
现在,虽然我同意第一条错误消息,但我也有一个变量已经超出范围?我仔细检查了我的对齐方式,似乎没有什么不合适的地方。
在 do 块中的 if 子句中分配变量的适当方法是什么?
【问题讨论】:
标签: haskell