Let vs be a regular job in GHCi

Is there a difference between

fx = x + 1 

and

 let fx = x + 1 

when entering into GHCi? IIUC there is a special rule that let without in inside GHCi effectively applied to the entire area of ​​the interpreter. But if let doesn't matter, what is the point of this rule?

Edit: Using GHCi version 8.0.2.

+5
source share
2 answers

No, no difference.

It used to be that ghci was essentially an open I / O unit. But the inability to define new types in this syntactic form and the need to write let for each definition were considered annoying restrictions that often found themselves in the way of everyday interactive use, and therefore the ghci syntax gradually became more permissive. But this is just a syntactic change - nothing profound.

However, there is one thing to be aware of: if you want to run a block, you must do it explicitly. For instance,

 > f [] = 3 > f (x:xs) = 4 

equivalently

 > let f [] = 3 > let f (x:xs) = 4 

but not

 > :{ | let f [] = 3 | f (x:xs) = 4 | :} 

therefore, there will be a new definition of f , which is the shadow of the old one and will be defined only for non-empty lists, while you probably should have given two equations for one f . With automatic block mode ( :set +m ), ghci may notice that let started the block and will automatically provide you with the last one when you enter let , this way:

 > let f [] = 3 | f (x:xs) = 4 | 

This will not be done to define short forms (not let ).

+12
source

Although this question was asked a few months ago, I notice one more difference when I start learning Haskell recently.

 > :set -Wall > a = 2 > let a = 3 

leads to a warning about name shadowing (-Wname-shadowing), and

 > :set -Wall > a = 2 > a = 3 

no. The warning seems to only check explicitly written let statements.

+1
source

Source: https://habr.com/ru/post/1265849/


All Articles