Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Nested Tuples: Turtles All the Way Down

Tuples can contain other tuples, which can contain still more tuples, ad infinitum (or at least until your computer’s memory gives up in despair). This allows you to create data structures of Byzantine complexity:

(set person
  #(person
    #(name #(first "Zaphod") #(last "Beeblebrox"))
    #(heads 2)
    #(occupation 'president)
    #(status 'froody)))

To extract deeply nested values, you can use nested pattern matching:

(let ((#(person #(name #(first fname) _) _ _ _) person))
  fname)
;; Returns: "Zaphod"

Those who have studied ancient scrolls will recognize this as remarkably similar to destructuring in other languages, except with more parentheses and a stronger emphasis on pattern correctness.