Scoobie
Scoobie

Reputation: 1139

Haskell multi-line `let` in `ghci`

I'm in ghci and find that the following works:

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

... but this doesn't work:

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

... nor does this:

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

The same error is provided when I try to compile it as a file, so it's not something that comes from being in ghci vs using ghc.

The error:

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

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

Upvotes: 3

Views: 1024

Answers (1)

Julia Path
Julia Path

Reputation: 2366

In GHCi you can use :{ :} for multiline expressions. For example:

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

The :{ :} keeps GHCi from evaluating your code after the next newline and throwing errors at you because it is not a complete expression.

Note also that the let is not needed for top-level definitions. In a normal Haskell source file you would just write:

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

Furthermore in newer GHCi versions (8.0+) you don't need the let either.

Upvotes: 5

Related Questions