Bindings
Edit on GitHubOn the Subject of let and let rec
The let and let rec statements introduce new names into the current scope.
In other words, they let you declare new bindings (quite similar to constants in other languages).
Syntax
1 | let name1 = value1 [, name2 = value2] |
Parameters
name1, name2, …, nameN
Names to be used for the binding identifiers.
value1, value2, …, valueN
Any valid Grain expression.
func1, func2, …, funcN
Any valid Grain functions.
Description
let allows you to introduce names, and let rec allows you to introduce recursive definitions.
Using let
Using let is pretty simple:
1 | # Assigning the value 5 to the name 'foo' |
let is scoped to the nearest block. That really just means it’s scoped to the nearest enclosing curly braces:
1 | let a = { |
Using let rec
let rec primarily allows you to define recursive functions.
The name of the identifier used will be within the scope of the body of the function:
1 | let rec fib = (n) => { |
let rec also allows you to define mutually recursive functions:
1 | let rec isEven = (n) => { |