04 Functional Stacks

The stack data structure is like a stack of cafeteria trays. Items get pushed onto the top, and off.

Let's talk about this

This is essentially the same as a list.

Now we have the same thing, but implemented with lists!

Let's compare Java and OCaml:

Stack s = new Stack();
s.push(1);
let s = MyStack.empty;;
let s' = MyStack.push 1 s;;

Note here that when we create the stack, we don't use the new keyword. Also, we don't write s.push, we put s as a parameter. What's interesting, is the Java Compiler will actually change s.push(1) to push(1, s), or something similar, so under the hood, it's not that different.