19 Mutable Singly Linked Lists (Part 1)

Let's bring in our code from last time.

What if we want to add a function that inserts an element at the first value.

Let's give it a try!

But we can simplify this! That's the same :)

What if we wanted to create a function that makes an empty list, instead of a singleton list. Our first instinct might be to do something like this

Awesome! Let's test it

Awesome, it seems to be working. Now let's make a second list:

Uh oh, since list is mutable, and we're mutating it, we're actually changing the empty list. To fix this, we need to rewrite empty as a function that takes in a unit.

Now, every time empty is called, we get a new empty list: