← Starting Henceforth

Episode 2

The Stack

Numbers live in a pile — newest on top, newest off first.

1:14 · Vivaldi · Presto (RV 315 iii)

Watch here · code in the app

Henceforth is the real terminal — one purchase ($9.99) for iPhone, iPad and Mac. Type the commands below into the app as you watch.

Code along

Tap to copy, then type it into Henceforth.

What you’ll learn

  • Numbers pile up on a stack — the newest is always on top.
  • Operators take from the top: + eats the top two, . prints the top.
  • Take one too many and you hit underflow — the stack only returns what you put in.
Transcript
  • but where do the numbers go? onto a pile.
  • a 3. a 4 on top. a 5 — newest always on top.
  • that's the stack — a pile of numbers, just waiting.
  • now watch + eat from it.
  • + takes the top two — the 5 and the 4 — and leaves a 9.
  • again — 3 and 9. now just 12.
  • . prints the top. there's the 12.
  • you only ever touch the top.
  • last on, first off — like a can of tennis balls.
  • let's empty one right out. three on the pile, 30 on top.
  • 30… 20… 10. empty.
  • now — one more than we put in. nothing's there.
  • underflow. you hit the floor — and see, it didn't say ok.
  • the stack gives back exactly what you put in.
← Ep 1Ep 3 · Why the maths looks backwards — soon