【问题标题】:Parsing error on premade code with WinGHCi使用 WinGHCi 对预制代码进行解析错误
【发布时间】:2018-01-27 03:38:42
【问题描述】:

我刚刚下载了完整的 Haskell 平台,并从我的老师那里输入了一些代码来尝试破解,但事实证明它(或其他东西)已经被破解了,因为当我评估 main 表达式时,我得到了一个解析错误。

data Shape = Circle Double | Rect Double Double
area :: Shape -> Double
area (Circle radius) = 3.1415 * radius * radius
area (Rect width height) = width * height

错误

<interactive>:17:56: error: parse error on input ‘::’

我的安装有问题吗?我正在运行 8.2.2 版

编辑:这是直接从 WinGHCi 窗口逐字复制粘贴的错误

GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
Prelude> data Shape = Circle Double | Rect Double Double
area :: Shape -> Double
area (Circle radius) = 3.1415 * radius * radius
area (Rect width height) = width * height

<interactive>:7:54: error: parse error on input ‘::’

【问题讨论】:

  • 删除以area开头的行的缩进。
  • 同样的错误,没有骰子。
  • 您确定生成错误的行是您包含的行之一吗?
  • 请附上minimal reproducible example。不清楚您是否包含了正确的代码。
  • 我编辑了我的问题以包含完整和逐字的文本屏幕 + 错误代码。这行得通吗?

标签: haskell parse-error


【解决方案1】:

GHCi 是一个 Read Eval Print Loop (REPL),因此它会在输入时评估每一行。而area是一个多行表达式,所以如果你尝试逐行输入,它会因为不完整而失败。

可以在 GHCi 中输入多行表达式。您需要做的是进入“多行模式”,输入您的行,然后再次存在多行模式。使用:{ 启动此编辑模式,然后使用:} 再次关闭它。这是一个完整的例子:

Prelude> data Shape = Circle Double | Rect Double Double
Prelude> :{
Prelude| area :: Shape -> Double
Prelude| area (Circle radius) = 3.1415 * radius * radius
Prelude| area (Rect width height) = width * height
Prelude| :}
Prelude> area $ Circle 1.1
3.8012150000000005
Prelude> area $ Rect 1.2 3.4
4.08

如您所见,Shape 的定义已经是单行了,所以我当时觉得不需要进入多行模式。但是,在输入area 的定义时,必须使用:{:}

不过,一般而言,GHCi 用于实验和快速反馈。您应该在 .hs 文件中编写 Haskell 源代码,通常使用 StackCabal 作为项目系统。

【讨论】:

    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多