Haskell安全读取文件
我正在尝试编写一个简单的Haskell函数,该函数读取文件内容(如果文件存在),而不执行其他操作Haskell安全读取文件,haskell,io,Haskell,Io,我正在尝试编写一个简单的Haskell函数,该函数读取文件内容(如果文件存在),而不执行其他操作 safeRead :: String -> IO () safeRead path = readFile path `catch` handleExists where handleExists e | isDoesNotExistError e = return () | otherwise = throwIO e 但是,由于:解析错误(可能是不正确的缩进或不匹配的括号
safeRead :: String -> IO ()
safeRead path = readFile path `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e
但是,由于:解析错误(可能是不正确的缩进或不匹配的括号)
为什么??我已经仔细检查了好几次缩进,我觉得一切都很好?在其他情况下,您错过了一个
=
:
safeRead :: String -> IO ()
safeRead path = readFile path `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e
正如塞巴斯蒂安所说,“|”必须通过
手柄,否则:
safeRead :: String -> IO ()
safeRead path = readFile path `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e
正如塞巴斯蒂安所说,“|”必须通过handleExists
h
您有两个错误
正如Daniel Sanchez指出的,一个是在之后缺少一个=
,否则
另一个是handleExists
的大小写缩进必须大于函数名,而不是where
。换言之,将两个|
向右移动到handleExists
的h
之外
演示:您有两个错误
正如Daniel Sanchez指出的,一个是在之后缺少一个=
,否则
另一个是handleExists
的大小写缩进必须大于函数名,而不是where
。换言之,将两个|
向右移动到handleExists
的h
之外
演示:虽然这显然是正确的(感谢您花时间指出!),但这不会导致我的错误消息消失(甚至更改)。为了避免将来的混淆,我编辑了我的问题以修复语法让我先检查一下,虽然这显然是正确的(感谢您花时间指出!),但这不会导致我的错误消息消失(甚至更改)。为了避免将来的混淆,我编辑了我的问题以修复语法让我先检查一下,谢谢对|
缩进的澄清,现在我知道了:)也谢谢“jsfiddle of haskell”链接。这会派上用场的。感谢您对|
缩进的说明,现在我知道:)也感谢“JSFIDLE of haskell”链接。那会派上用场的。