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

后端 未结 1 1000
梦谈多话
梦谈多话 2021-02-05 14:39

I do not understand the difference between the three syntaxes:

  • where a = f (b)
  • do a <- f (b)
  • do let a
相关标签:
1条回答
  • 2021-02-05 15:05

    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 ...
    
    0 讨论(0)
提交回复
热议问题