2016-01-08 18 views
0

我正在尝试编写一个简单的Haskell函数,读取文件内容,如果该文件存在,否则什么也不做。文件的哈斯克尔saferead

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)

为什么?我已经多次检查过缩进,而且我看起来都很好?

回答

2

您有两个错误。

正如Daniel Sanchez指出的那样,您在otherwise之后错过了=

另一个是handleExists的情况下,必须缩进比功能名称更多,而不是where。换句话说,将两个|移到handleExistsh之后。

演示:http://goo.gl/EY2c7o

+0

感谢澄清'|'的缩进,现在我知道:)也感谢“jsfiddle of haskell”链接。这会派上用场。 –

2

你错过了=后否则:

safeRead :: String -> IO() 
safeRead path = readFile path `catch` handleExists 
    where handleExists e 
      | isDoesNotExistError e = return() 
      | otherwise = throwIO e 

塞巴斯蒂安说,在 '|'必须通过handleExistsh

+0

,而这显然是真实的(感谢你抽出指出来的时间!),这不会导致我的错误信息消失(或者甚至改变)。为了避免混淆,我编辑了我的问题来修复语法 –

+0

让我proppercheck,然后 – Netwave