Haskell在`ghci`中多行`let`

时间:2016-12-02 13:23:55

标签: list haskell ghc multiline ghci

我在ghci中发现以下情况:

let foo = ["a", "b", "c"]

......但这不起作用:

let bar = ["a",
           "b",
           "c"]

......也不是这样:

let baz = ["a"] ++
          ["b"] ++
          ["c"]

当我尝试将其编译为文件时,会提供相同的错误,因此不会出现ghci与使用ghc相关的错误。

错误:

[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:3:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)

1 个答案:

答案 0 :(得分:4)

在GHCi中,您可以将:{ :}用于多行表达式。例如:

Prelude> :{
Prelude| let bar = ["a",
Prelude|            "b",
Prelude|            "c"]
Prelude| :}

:{ :}阻止GHCi在下一个换行符后对您的代码进行评估并向您抛出错误,因为它不是一个完整的表达式。

另请注意,顶级定义不需要let。在普通的Haskell源文件中,您只需编写:

bar = ["a",
       "b",
       "c"]

此外,在较新的GHCi版本(8.0+)中,您也不需要let

相关问题