Linguistic Meaning of 'Let' Variable in Programming

Linguistic meaning of 'let' variable in programming

It comes from the English word 'let'.

verb: "let", "letting".
1.
to allow or permit:

// Hey computer, can you please
let
// this
night = 'wonderful'

Lisp has the keyword let and it's been around since 1958, though it may have come from even earlier.

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. Variables are considered low level entities not suitable for higher levels of abstraction, thus the desire of many language designers to introduce similar but more powerful concepts like in Clojure, F#, Scala, where let might mean a value, or a variable that can be assigned, but not changed, which in turn lets the compiler catch more programming errors and optimize code better.

JavaScript has had var from the beginning, so they just needed another keyword, and just borrowed from dozens of other languages that use let already as a traditional keyword as close to var as possible, although in JavaScript let creates block scope local variable instead.

What is the difference between `let` and `var` in Swift?

The let keyword defines a constant:

let theAnswer = 42

The theAnswer cannot be changed afterwards. This is why anything weak can't be written using let. They need to change during runtime and you must be using var instead.

The var defines an ordinary variable.

What is interesting:

The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.

Another strange feature:

You can use almost any character you like for constant and variable
names, including Unicode characters:

let = "dogcow"

Excerpts From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=881256329



Community Wiki

Because comments are asking for adding other facts to the answer, converting this to community wiki answer. Feel free edit the answer to make it better.

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

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.

Difference between define, let and set!

Do you mean (+ 1 a) instead of (1+ a) ? The latter is not syntactically valid.

Scope of variables defined by let are bound to the latter, thus

(define (f x)
(let ((a 1))
(+ a x)))

is syntactically possible, while

(define (f x)
(let ((a 1)))
(+ a x))

is not.

All variables have to be defined in the beginning of the function, thus the following code is possible:

(define (g x)
(define a 1)
(+ a x))

while this code will generate an error:

(define (g x)
(define a 1)
(display (+ a x))
(define b 2)
(+ a x))

because the first expression after the definition implies that there are no other definitions.

set! doesn't define the variable, rather it is used to assign the variable a new value. Therefore these definitions are meaningless:

(define (f x)
(set! ((a 1))
(+ a x)))

(define (g x)
(set! a 1)
(+ a x))

Valid use for set! is as follows:

(define x 12)
> (set! x (add1 x))
> x
13

Though it's discouraged, as Scheme is a functional language.

What is the difference between let and var?

Scoping rules

The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

function run() {
var foo = "Foo";
let bar = "Bar";

console.log(foo, bar); // Foo Bar

{
var moo = "Mooo"
let baz = "Bazz";
console.log(moo, baz); // Mooo Bazz
}

console.log(moo); // Mooo
console.log(baz); // ReferenceError
}

run();


Related Topics



Leave a reply



Submit