Difference between where bindings, let bindings and the single assignment operator (<-)

江枫思渺然 提交于 2020-05-24 21:22:27

问题


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?


回答1:


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...elses. 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 ...


来源:https://stackoverflow.com/questions/9254124/difference-between-where-bindings-let-bindings-and-the-single-assignment-operat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!