What is the "let" keyword in functional languages like F# and OCaml for? -


When looking at F #, Ocaml and other functional language code examples, I notice that let's use the keyword many times goes.

  • Why do you need it? Why the languages ​​were designed to do this?
  • Why can not you leave it out? Example: Let's get x = 4x = 4

F # (and Okmeml ) There is a powerful build that is used for value binding , which means giving some meaning to the symbol. It can mean different things:

Announcing local or global values ​​ - You can use it to declare local values. It is similar to creating a variable in mandatory languages, with the exception that the value of the variable can not be changed later (it is irreversible):

  Hello = "Hello World" printfan "%" Hello  

Declaration of Functions - You can also use it to declare the function. In this case you specify that a symbol is a function with some iriate:

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

Why do you need it? In F #, the code will be obscured without it. You can use hidden values ​​ to create new symbols, which hides the previous symbol (with the same name), for example the following returns true :

  let's test () = let x = 10 let x = 20 / hides compare with 'x' x = 20 // '20 'x' and result of return  

If you leave the Walk keyword, you do not know whether you are comparing prices or whether you're announcing a new symbol, as well as other Noted by people, you can let Syntax (if you use line breaks in F #, you do not need to type in to write value binding as part of any other expression): < / P> < z = (x = 3 + 3x * x)

Here, the value of z will be 36. Although you might be able to invent some syntax for which the let keyword is not required, I think that using the code Let make the code even more readable gives.


Comments