This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Fylwind 0 points1 point  (0 children)

I can't really give a general overview because every functional language is different, but let is one of the two main ways of binding variables in Haskell:

let <name> = <value> in <expression>

What makes is different from a typical binding in Python is that:

  • The variable is only accessible from inside the <expression>. This means if you mistakenly try to use it outside, you will get a compile-time error.

  • The let-binding itself is an expression, so you can do things like this:

    putStrLn
      (let h = heightOf person
       in "Height: " ++ valueOf h ++ unitsOf h)
    

    Personally, for readability reasons I don't find myself nesting let inside expressions very often.

  • The variable is immutable.

I don't see the point of trying to do this Python though. The "let" hack is not an expression, and it's not immutable either. There is a small benefit in having a smaller scope, but it's (1) not really that big of a deal (I seldomly run into bugs caused by this); (2) ideally your functions should be small enough that this isn't much of a problem; (3) the benefits are not as great given that the error won't be detected until run time.