(chapter-start 'list-manipulation "utilities for manipulating and iterating over lists, including filters and transforms") (def zip (a b) ; takes two lists, (p q r) and (1 2 3), returns ((p 1) (q 2) (r 3)) (if a (cons (list (car a) (car b)) (zip (cdr a) (cdr b))))) (def eachr (f things) (when things (eachr f (cdr things)) (f (car things)))) (mac push (x things) `(= ,things (cons ,x ,things))) (def flatten (things) (let acc nil (rfnwith flattenize (x things) (if (pair? x) (eachr flattenize x) (push x acc))) acc)) (def assoc (key al) ; given a list 'al of form '( (k0 v0) (k1 v1) (k2 v2) ... (kn vn) ) and ; a 'key, returns the list (kx vx) from 'al where kx is equal to 'key ; #attribution: inspiration from arc.arc (if (pair? al) (if (caris key (car al)) (car al) (assoc key (cdr al))))) (def alref (key al) ; given a list 'al of form '( (k0 v0) (k1 v1) (k2 v2) ... (kn vn) ) and ; a 'key, returns vx from 'al where kx is equal to 'key ; #attribution: lifted almost directly from arc.arc (cadr (assoc key al)))