Prelude Data.Set> :load hello
[1 of 1] Compiling Main ( hello.hs, interpreted )
hello.hs:11:11: parse error on input `<-'
Failed, modules loaded: none.
Prelude Data.Set> h <- IO.openFile "testtext" IO.ReadMode
Prelude Data.Set>
hello.hs 中的同一行 [h <- IO.openFile "testtext"IO.ReadMode] 会引发错误。我该如何解决?我究竟做错了什么?
[编辑]
来源和输出:http://pastebin.com/KvEvggQK
最佳答案
您只能使用 <-
在 do
内-block¹(您隐含在 GHCI 中,但不在 Haskell 文件中)。
在 Haskell 文件中,您只能使用 =
编写绑定(bind)。 .
您可以做的是将以下内容放入 Haskell 文件中:
myHandle = do h <- IO.openFile "testtext" IO.ReadMode
return h
尽管如果您考虑一下,这与以下内容相同:
myHandle = IO.openFile "testtext" IO.ReadMode
但是这样
myHandle
仍然包裹在 IO
中你需要<-
(或 >>=
)在 ghci 中打开它。您不能以这样的方式编写 Haskell 文件,即仅加载文件就会打开
testtext
并给你文件句柄。¹ 或列表理解,但有
<-
的正确操作数需要是一个列表,所以这与你的情况无关。
关于haskell - 如何在haskell中修复 "parse error on input"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4482409/