Changing Immutable Objects

Learn about ways to process immutable data.

Changing immutable objects

We do sometimes need to “change” immutable objects. Of course, you can’t actually do that, but what you can do is create a copy of the original object, modified in some way. There are various ways of doing this, which we will explore here.

Let’s start with a simple example. The tail function takes a list and returns a list that is identical except that the first term is removed. So, [1, 2, 3] becomes [2, 3]. Here is how we might do this:

Get hands-on with 1200+ tech skills courses.