Chapter 4 · Your own data

Lists and recursion

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A list holds values of one type

In this lesson you write functions on lists, and prove facts about given lists.

A list holds values of one type, in order. List Nat is the type of lists of numbers:

listwhat it holds
[1, 2, 3]the numbers 1, 2 and 3, in that order
[5]one number
[]nothing: the empty list

#eval [1, 2, 3] prints [1, 2, 3].

#eval [1, 2] ++ [3, 4]
#eval [1, 2, 3].length
#eval [1, 2, 3].reverse
def front (x : Nat) (xs : List Nat) : List Nat := xs
theorem len_front (x : Nat) (xs : List Nat) :
    (front x xs).length = xs.length + 1 := by
  rfl
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
#eval sum [5, 6]
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
#eval len [5, 6]
example : len [5, 6, 7] = 3 := by
  simp [len]
-- doubleAll: every number of the list times 2
-- then show doubleAll [1, 2, 3] with #eval
def doubleAll : List Nat → List Nat
  | [] => []
  | x :: xs => sorry
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => app xs ys
#eval app [1, 2] [3]
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
def app : List Nat → List Nat → List Nat
  | [], ys => ys
  | x :: xs, ys => x :: app xs ys
theorem sum_three : sum [1, 2, 3] = 6 := by
  sorry
theorem app_one : app [1] [2, 3] = [1, 2, 3] := by
  sorry
theorem len_app_two (ys : List Nat) :
    len (app [1, 2] ys) = 2 + len ys := by
  sorry
def len : List Nat → Nat
  | [] => 0
  | _ :: xs => 1 + len xs
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
-- every number of the list times itself
def squares : List Nat → List Nat := sorry
-- show squares [1, 2, 3] with #eval
theorem len_squares : len (squares [1, 2, 3]) = 3 := by
  sorry
theorem sum_squares : sum (squares [1, 2, 3]) = 14 := by
  sorry
theorem len_squares_cons (x : Nat) (xs : List Nat) :
    len (squares (x :: xs)) = 1 + len (squares xs) := by
  sorry
-- the last number of xs, or d when xs is []
def lastOr (d : Nat) : List Nat → Nat := sorry
theorem last_six : lastOr 0 [4, 5, 6] = 6 := by
  sorry
theorem last_none : lastOr 7 [] = 7 := by
  sorry
Terminal
Step 1 of 9See it run

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)