List prepend

Note that lists are great when we insert a node at the beginning, in other words, prepend a value to the head of a list. Let's see how that works.

We have the list with values 17, 199, and 337. We prepend the value 37 first. Next, we prepend 99 to the resulting list. Finally, we prepend 12 to the new resulting list again.

When we prepend the value 37, we just allocate the node; while constructing this node, we append the original list to 37. In other words, 37 becomes the head and the original list becomes the tail.

List prepend

Note that there is no copying needed at all. We just allocate the new node and append the existing list to it. As this ...

Get Learning Functional Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.