为什么我不能在 ghci 中定义新类型?
当我尝试定义新类型时,在 ghci 中出现错误:
Prelude> data Point = Pt Int Int <interactive>:1:0: parse error on input `data' Prelude> let data Point = Pt Int Int <interactive>:1:4: parse error on input `data'
我做错了什么?
I get an error in ghci when I try to define a new type:
Prelude> data Point = Pt Int Int <interactive>:1:0: parse error on input `data' Prelude> let data Point = Pt Int Int <interactive>:1:4: parse error on input `data'
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
titaniumdecoy
,我记得当我了解到经常提到的一点时,在 GHCi 中编写诸如 'let square x = x * x' 之类的东西就像编写let a = f b
在IO
monad 中使用do
表示法——在这种例子中说:同样,当你在 GHCi 中重新定义一个表达式时,它有点像就像使用
do
表示法执行以下操作一样,这是完全合法的:没有人会在这样的序列中间声明数据类型,但会在模块的其他地方执行此操作。我可能猜到存在某种理论上的反对意见,但唐·S. 的评论表明不存在这种反对意见。
titaniumdecoy
, I remember being helped with this sort of GHCi mystery when I learned the frequently made point that writing things like 'let square x = x * x' inside the GHCi is like writinglet a = f b
withdo
notation in theIO
monad -- say in this sort of example:Similarly, when you redefine an expression in the GHCi, it's sort of like doing the following in
do
notation, which is perfectly legitimate:No one would declare a data type in the middle of such a sequence, but would do it elsewhere in the module. I might have guessed that there was some sort of theoretical objection, but Don S.'s remark suggests there isn't one.
这是可能的GHC 7.4.1。
It is possible since GHC 7.4.1.
ghci 不允许您从交互式输入定义类型 - 相反,您需要将类型定义放入文件中,然后
:load
该文件到 ghci 中。ghci does not allow you to define types from interactive input - instead, you need to put your type definition in a file and
:load
the file into ghci.仅供历史参考,HBI Haskell 交互环境 允许完整的 Haskell命令行,包括类型、类等。没有先验的 GHCi 不能进行类似的操作,用户可以编写支持此功能的 GHC-API 前端......
Just for historical reference, the HBI Haskell interactive environment allows for full Haskell at the command line, including types, classes and so on. There's no a priori GHCi can't operate similarly, and users could write a front-end to GHC-API that supported this...