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

The Prelude

lib/prelude.naj is loaded into the Purple session at boot. Five list functions, written in pure λ↑↓ — unary lambdas, so multi-argument functions are curried, and the empty list is 'nil. Nothing here is special-cased: each is an ordinary defined binding, visible with the rest of the session state.

((append xs) ys)

The concatenation of two lists:

((append '(1 2)) '(3 4))
;=> (1 2 3 4)
((append 'nil) '(a b))
;=> ('a 'b)

((map f) xs)

The list of (f x) for each element:

((map (lambda (n) (* n n))) '(1 2 3))
;=> (1 4 9)

f may be any applicable value — a session closure as here, a floor closure, a curried prelude function.

((assoc k) alist)

The first pair in alist whose car is k, or nil:

((assoc 'b) '((a 1) (b 2)))
;=> ('b 2)
((assoc 'z) '((a 1) (b 2)))
;=> nil

Note the result is the whole matching pair, not its value, and that the alist here is a list of two-element lists — recall from Part I that dotted pairs cannot be written under a quote.

(length xs)

(length '(a b c))
;=> 3
(length 'nil)
;=> 0

(reverse xs)

(reverse '(1 2 3))
;=> (3 2 1)

Accumulator-based, so linear in the list.