With that, we have everything we need to represent the lists. A random access list will be defined as follows:

ralist := empty
|| (size tree) :: ralist

Using that definition (and Scheme’s standard empty lists for the empty ralist as well), null? is each enough to define:

(define (*null? ra)
(null? ra))

cons is a little more interesting as we have to deal with potentially merging the first two items in the list to preserve the O(log(n)) access to the list. It’s still O(1) overall for the cons though. Here’s where the power of pattern matching really shines though.

car and cdr (they were named head and tail in the original paper which really makes more sense, but I wanted to keep to the Scheme naming convention) aren’t bad at all, the only interesting part is taking the cdr of a list will split the first tree.

The last functions presented in the paper are lookup and update, which have been given their Scheme names of list-ref and list-set (note: not list-set! since the update is purely functional and no mutation is occurring). Again, very straight forward.