15

When looking at F#, Ocaml and other functional language code examples I notice that the let keyword is used very often.

  • Why do you need it? Why were the languages designed to have it?
  • Why can't you just leave it out? e.g: let x=4 becomes x=4
Trix
  • 221
  • 3
  • 5

6 Answers6

27

In F# (and OCaml) let is quite powerful construct that is used for value binding, which means assigning some meaning to a symbol. This can mean various things:

Declaring local or global value - you can use it for declaring local values. This is similar to creating a variable in imperative languages, with the exception that the value of the variable cannot be changed later (it is immutable):

let hello = "Hello world"
printfn "%s" hello

Declaring function - you can also use it for declaring functions. In this case you specify that a symbol is a function with some arity:

let add a b = a + b
printfn "22 + 20 = %d" (add 22 20)

Why do you need it? In F#, the code would be ambiguous without it. You can use value hiding to create new symbol that hides the previous symbol (with the same name), so for example the following returns true:

let test () =
  let x = 10
  let x = 20 // hides previous 'x'
  x = 20     // compares 'x' with 20 and returns result

If you omitted the let keyword, you wouldn't know whether you're comparing values or whether you're declaring a new symbol. Also, as noted by others, you can use the let <symbol> = <expression> in <expression> syntax (if you use line-break in F#, then you don't need in) to write value bindings as part of another expression:

let z = (let x = 3 + 3 in x * x)

Here, the value of z will be 36. Although you may be able to invent some syntax that doesn't require the let keyword, I think that using let simply makes the code more readable.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 3
    It seems that `let` is introduced only because `x = y` is ambiguous? If `==` is used for comparison then does the `let` becomes unnecessary? – kennytm May 16 '10 at 18:58
  • 1
    @KennyTM: In principle, you could definitely invent some non-ambiguous syntax, but I think that `let` is quite readable. Also, in the full syntax, the `let` is coupled with `in` (because it is an expression - there are no statements in F#), so you'd need something similar. – Tomas Petricek May 16 '10 at 20:19
  • 1
    I shiver when I see `let` w/out `in` in F#... As an ocaml programmer I see the `in` being a huge help in readability and formatting. Why is it not used as much by F# programmers? – nlucaroni May 19 '10 at 14:02
  • 1
    @nlucaroni You can use the OCaml 'in' syntax in F# by omitting the "#light" option at the beginning of the file. F# designers thought ocaml syntax a little verbose and gave users the option to have a #light-er one. Scope in #light syntax is given by indentation. When you get used to it it's pretty nice :-) – Mau Jun 06 '10 at 13:45
9

The main purpose of "let" is putting a scope around its definitions.

let <definitions> in <expression>

Makes sure that the definitions don't pollute the namespace of anything other than <expression>.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Peaker
  • 2,354
  • 1
  • 14
  • 19
4

"let" introduces a new variable scope, and allows you to bind variables to values for that scope. It is often read as "let x be [value] in ...". When you don't have assignment, it is very useful to avoid conflicting variable names.

JesperE
  • 63,317
  • 21
  • 138
  • 197
3

In Haskell,

foo = let x = 5
      y = 7
      in z=x+y

is used to make clear that x and y are "private" variables to foo.

ptikobj
  • 2,690
  • 7
  • 39
  • 64
1

let means "bind value to name" and generally spoken, creates a new variable. x = 4 means "assign 4 to x", which doesn't create a new name.

See http://msdn.microsoft.com/en-us/library/dd233238.aspx

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Might be traditional from Lisp, where they don't use `=` and infix syntax in general. – Anton Tykhyy May 16 '10 at 14:29
  • 1
    Actually in both F# and ocaml `x = 4` is a boolean operation that returns true iff x is equal to 4. It's not assignment. – sepp2k May 16 '10 at 15:18
1

If you're into learning, this lecture might be relevant also, esp. the part about evaluating the let term.