为什么我不能在 ghci 中定义新类型?

发布于 2024-09-06 19:51:32 字数 241 浏览 1 评论 0原文

当我尝试定义新类型时,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大姐,你呐 2024-09-13 19:51:32

titaniumdecoy,我记得当我了解到经常提到的一点时,在 GHCi 中编写诸如 'let square x = x * x' 之类的东西就像编写 let a = f bIO monad 中使用 do 表示法——在这种例子中说:

palindromize :: IO ()
palindromize = do
  a <- readFile "foo.txt"
  let b = reverse a
  putStrLn (a ++ b)

同样,当你在 GHCi 中重新定义一个表达式时,它有点像就像使用 do 表示法执行以下操作一样,这是完全合法的:

makeMess :: IO ()
makeMess = do
   a <- readFile "foo.txt"
   let b = reverse a
   putStrLn (a ++ b)
   let b = a
   putStrLn (a ++ b)

没有人会在这样的序列中间声明数据类型,但会在模块的其他地方执行此操作。我可能猜到存在某种理论上的反对意见,但唐·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 writing let a = f b with do notation in the IO monad -- say in this sort of example:

palindromize :: IO ()
palindromize = do
  a <- readFile "foo.txt"
  let b = reverse a
  putStrLn (a ++ b)

Similarly, when you redefine an expression in the GHCi, it's sort of like doing the following in do notation, which is perfectly legitimate:

makeMess :: IO ()
makeMess = do
   a <- readFile "foo.txt"
   let b = reverse a
   putStrLn (a ++ b)
   let b = a
   putStrLn (a ++ b)

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.

開玄 2024-09-13 19:51:32

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.

泪之魂 2024-09-13 19:51:32

仅供历史参考,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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文