In Part 1, we gave names to values using def. Now we place parameters after the name to build functions! Open an empty Functions.lean file, append each section’s Lean code block to the same file in order, save after each section, and run lean Functions.lean to compare with the sample output (only the newly added output lines are shown in each section).
1. Calling Functions with Spaces Instead of Parentheses
In C or Python you call a function with parentheses and commas like add(3, 5), whereas in ML- and Haskell-family functional languages, including Lean, you call a function simply by writing its arguments after the function name separated by spaces.
In the definition below, (a b : Nat) groups two inputs of the same type together. The : Nat after it is the return type of the function, and the calculation goes to the right of :=.
In the definition, n is the parameter, the name we give the input. In double 7, the actual number 7 is the argument. Pass another number and the same body, n * 2, runs with a different value for n.
def double (n : Nat) : Nat :=
n * 2
def add (a b : Nat) : Nat :=
a + b
#eval double 7
#eval add 3 5
#check addOutput:
14
8
add (a b : Nat) : NatThree lines come back from the file. The first line, 14, is the result of double 7 substituting 7 for n and computing 7 * 2, while the second line, 8, is the result of add 3 5 adding a = 3 and b = 5. The third line, add (a b : Nat) : Nat, is the constant signature printed by #check add, showing that add takes two natural-number parameters a and b and returns a Nat.
One important rule to keep in mind is that function application (a space) binds tighter than arithmetic operators (+, *). Writing double 3 + 1 therefore evaluates as (double 3) + 1 and gives 6 + 1 = 7; to pass the entire sum 3 + 1 as the argument, wrap the compound argument in parentheses as double (3 + 1) to get 8.
2. Currying and Partial Application
In Section 1, #check add displayed the declared parameter signature add (a b : Nat) : Nat, but if you wrap it in parentheses as #check (add) to ask for the type of the expression itself, Lean prints add : Nat → Nat → Nat.
Arrows associate to the right as Nat → (Nat → Nat): feeding the first natural number a returns a function (Nat → Nat) that waits for the second natural number b. Representing a multi-argument function as a chain of single-argument functions is called currying. Thanks to currying, you can write add 10 to create a reusable “always add 10” function without rewriting addition.1
Two symbols drive the notation here: and (the Greek letter “lambda”) in the formula below. An arrow between types means “a function that takes a value of type and returns a value of type ,” and means “an anonymous function that takes an input and returns ” (written fun b => 10 + b in Lean code).
Thus takes the first input and returns the function , which remembers while waiting for the second input .
Whenever several places in your code need to add 10, you can pass this function around directly. Unlike a finished numeric sum, add 10 is still a function waiting for one more argument.2
The arrows in the type group from the right, but the call add 10 25 groups from the left as (add 10) 25. The first step gives you a function; passing 25 to that function gives you the sum.
def addTen : Nat → Nat := add 10
#eval addTen 25
#eval 5 |> (fun x => x * 3) |> addTenOutput:
35
25Both output lines follow directly from partial application. The first line, 35, passes 25 to addTen to compute 10 + 25. Second, 5 |> (fun x => x * 3) |> addTen uses the pipeline operator |> to feed each value on the left into the function on its right: starting from 5, the tripling function fun x => x * 3 produces 15, which then flows into addTen to yield 15 + 10 = 25.
fun x => x * 3 corresponds to . The arrows below trace how values travel through the pipeline rather than function types. Even without naming intermediate variables, you can read the value from the left flowing into the function on the right.3
3. Organizing Intermediate Steps with let and if-then-else
The function below doubles its input and checks whether the result exceeds an upper limit. Binding the doubled value with let name := expr lets you reuse it on the very next line, and if cond then val1 else val2 picks which value to return according to the condition. Because an if expression must always produce a value, we include the else branch so it returns the limit when exceeded and the doubled value otherwise.
doubled is a local name you can use in the rest of this function body. The two branches of if must produce values of the same type; here both limit and doubled are Nat.
In mathematical terms, the intermediate value is bound in code by let doubled := x * 2. We reuse doubled both when comparing against the limit (limit) and when returning the value unchanged, so the final result equals , the smaller of the two numbers.
That is why the first output line, clampHigh 20 7, returns 14 unchanged because 7 * 2 = 14 is within the limit 20, whereas the second line, clampHigh 20 15, clamps 15 * 2 = 30 at the ceiling and prints 20.4
def clampHigh (limit x : Nat) : Nat :=
let doubled := x * 2
if doubled > limit then limit else doubled
#eval clampHigh 20 7
#eval clampHigh 20 15Output:
14
20Numbers and strings alone are rarely enough to represent real domain states. In Part 3, we will define structures (structure) that bundle multiple fields together and inductive types (inductive) that choose among several distinct cases.
Footnotes
- David Thrane Christiansen. Functional Programming in Lean. Functions and Definitions · Lean Language Reference 4.36.0. Checking Types. Explains signature output when applying
#checkto a constant name and partial application of arrow function types. ↩ - Lean Language Reference 4.36.0. Functions: λ Abstraction · Function Application.
funcreates lambda abstractions and juxtaposition performs function application. ↩ - Lean Language Reference 4.36.0. Function Application: Pipeline Syntax · Functions: λ Abstraction. Covers argument flow in pipeline syntax and anonymous functions created with
fun. ↩ - David Thrane Christiansen. Functional Programming in Lean. Functions and Definitions. Provides examples of binding intermediate expressions with
letand branching withif. ↩