Skip to content

Small programs, and #eval everything

18 June 2025

Lean is a programming language first, and #eval runs anything right in the editor, which makes it a pleasant place for small programs.

structure Point where
  x : Int
  y : Int
  deriving Repr

def Point.add (p q : Point) : Point :=
  { x := p.x + q.x, y := p.y + q.y }

#eval Point.add { x := 1, y := 2 } { x := 3, y := -4 }
-- { x := 4, y := -2 }

deriving Repr is what lets #eval print a Point. Lists come with the usual functional toolkit, and |>. chains it left to right:

#eval (List.range 10).filter (· % 3 == 0) |>.map (· * 2)
-- [0, 6, 12, 18]

IO with do notation reads like an imperative language:

def main : IO Unit := do
  let stdin ← IO.getStdin
  let line ← stdin.getLine
  IO.print s!"you said: {line}"

getLine keeps the newline, hence print rather than println. lake exe turns the program into a native binary, since Lean compiles to C.