我试图编写一个简单的Haskell函数,如果文件存在,它将读取文件内容,而不执行其他操作。
safeRead :: String -> IO ()
safeRead path = readFile path `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e但是,这在以下方面失败了:parse error (possibly incorrect indentation or mismatched brackets)
为什么?我已经检查过几次压痕了,我觉得一切都还好吗?
发布于 2016-01-08 09:27:40
你有两个错误。
正如Daniel所指出的,第一个问题是,在=之后,您错过了一个otherwise。
另一种情况是handleExists的情况必须缩进多于函数名,而不是where。换句话说,将两个|移到h of handleExists之后的右侧。
演示:http://goo.gl/EY2c7o
发布于 2016-01-08 09:25:16
否则,您错过了一个=:
safeRead :: String -> IO ()
safeRead path = readFile path `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e就像塞巴斯蒂安说的那样,“x”必须通过handleExists h。
https://stackoverflow.com/questions/34673374
复制相似问题