I do not understand the difference between the three syntaxes:
where a = f (b)
do a <- f (b)
do let a = f (b)
I do understand somehow though that a <- f(b)
is different from the other two, in most cases where I tried all three worked. Also I read somewhere on the net that per block you should try to get along with one let binding only in order to be "idiomatic". But I never seem to manage.
How do I decide what to use?
let foo = bar in ...
simply defines foo
to be the exact same thing as bar
within the context of ...
; you could simply use textual substitution to replace all uses of foo
in ...
with (bar)
and get the exact same result.
where
clauses are similar to let...in
expressions, but go at the end of a function clause, instead of being an expression. For instance,
foo x
| p1 = ... y ...
| p2 = ... y ...
where
y = ...
There is no way to rewrite this with let...in
without changing the guards into if...then...else
s. Often, where
clauses are used over let...in
clauses purely for reasons of style.
The bind operator is something different entirely. It's used in do
notation to "extract" a value from a monadic computation. That is, if foo
has the type m a
, then after x <- foo
, x
has the type a
. All the other "binding" forms just define names, but <-
is used for binding a computation's result to a name from within a monad. <-
can only be used inside a do
block, so it's exclusively used to build up a larger computation in the same monad as the action you're binding the result of.
let foo = bar
in do
notation is just a convenience; you can rewrite:
do let foo = bar
...
as
let foo = bar
in do ...
but that gets messy when you have a lot of such bindings, as the do
blocks get nested deeper and deeper.
I don't know what the advice you mentioned is talking about; you can define multiple variables in a let...in
block just fine, and
let foo = ...
bar ...
in ...
is more idiomatic than
let foo = ...
in let bar = ...
in ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With