Stage 01 · Step 4 of 50

Words and collections.

Core Lean5 minutesLean 4.36.0

01The idea

A word can join another word. A row of items can join another row.

Lemmi · Your guide for this idea
Lemmi pointing

Lean keeps these two kinds of joining separate through their types.

02In maths

Joining lists preserves the order of their elements. The length counts how many elements a list contains.

Picture it

Joining text and joining lists with ++Top row, joining two strings: "Lean" ++ " Trail" gives "Lean Trail". Bottom row, joining two lists drawn as trains: a train with wagons 2 and 4, then ++, then a train with one wagon 6, gives one train with wagons 2, 4, 6 in that order. Counting the wagons of the joined train gives 1, 2, 3, so its length is 3.JOINING TEXT (String ++ String)"Lean"++" Trail""Lean Trail"JOINING LISTS (List Nat ++ List Nat)246246++[2, 4][6]123length 3same order: first train, then second

Swipe sideways to see the whole picture.

Picture a list as a train of wagons. ++ couples the second train behind the first, so the order stays 2, 4, then 6. length just counts wagons, which is why ([2, 4] ++ [6]).length is 3. The same ++ joins text too, but Lean only lets you join text to text and lists to lists.

03In Lean

This code comes directly from the file checked by Lean.

E04.leanTry it live (opens official Lean editor)
#eval "Lean" ++ " Trail"
#eval ([2, 4, 6] : List Nat).length
#eval ([2, 4] : List Nat) ++ [6]

example : (([2, 4] : List Nat) ++ [6]).length = 3 := by
  rfl

A string stores text. A list stores a sequence of elements of one type. An element is one item in that sequence.

Double quotation marks surround strings. Square brackets surround a list literal. Commas separate its elements.

The symbol ++ means append for these examples. Its meaning depends on the types of the things being joined.

The final claim checks the length after joining two lists. Once joined, the list has three entries.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 5
⊢ ([2, 4] ++ [6]).length = 3
After line 5: example : (([2, 4] : List Nat) ++ [6]).length = 3 := by
⊢ ([2, 4] ++ [6]).length = 3
After line 6: rfl
no goals

05Lean says

Captured from Lean 4.36.0
"Lean Trail"
3
[2, 4, 6]
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Set greeting to the text Hello followed by a space and Lean.

  2. A little stretch

    Set trail by joining the list containing one and two with the list containing three and four.

Exercises/E04.leanTry it live (opens official Lean editor)
def greeting : String := sorry

def trail : List Nat := sorry

08A quick check

1. What does the list length count?

Read the answer and explanation

The number of elements. Length counts entries, regardless of their values.

2. Does joining two lists change their element order?

Read the answer and explanation

No, it keeps both orders. The elements of the second list follow the elements of the first.

One more idea in your toolkit.

Take your time. Progress is yours to mark.