A very simple confusion about "let" when assigning variables

98 Views Asked by At

The wikibooks Haskell page on variables and functions notes the following:

Note

The let keyword (a word with a special meaning) lets us define variables directly at the GHCi prompt without a source file. This looks like:

Prelude> let area = pi * 5 ^ 2

Although sometimes convenient, assigning variables entirely in GHCi this way is impractical for any complex tasks. We will usually want to use saved source files.

but I can just do area = pi * 5 ^ 2 at the prompt and skip the let. I've also seen this use of let at the prompt in Learn You a Haskell. Why would I use the let, is there something important about it that I'm missing?

1

There are 1 best solutions below

2
AlexSchell On

GHCi evaluates the expression (the whole line) as soon as enter is pressed, and the expression itself cannot be spaned over several lines (to make an expression in GHCi span over multiple lines :{ and :} can be used).

In Haskell, a let expression should be followed by in. But in GHCi the expression is interpreted in the IO monad, and a let binding with no accompanying in statement can be just be followed by an empty line.

Since GHC 8.0.1 it is possible to bind values and functions to names without let statement.

Important difference, however, between these two types of binding and a third type, the monadic bind, is that the monadic bind a <- exp is strict, so exp is evaluated immediately, while with the let form or direct equality form exp would not be evaluated immediately.