Table of Contents
- Background: What is a Cons cell?
- What it might look like in Scala
- Starting to create my own Cons class
- My second effort
- Defining my nil value
- Defining Cons
- Replacing the NilCons method bodies
- Adding a toString method to Cons
- The complete code at this point
- I’d really like a :: method
- See also
For some examples in my new book on functional programming in Scala I needed to create a collection class of some sort. Conceptually an immutable, singly-linked list is relatively easy to grok, so I decided to create my own Scala list from scratch. This tutorial shows how I did that.Back to top
Background: What is a Cons cell?
The first time I learned about linked lists was in a language named Lisp. In Lisp, a linked list is created as a series of “Cons” cells. A cons cell is simple, it contains only two things: