(hash-set macs 'if (fn args (cond args (cond (cdr args) (cond (cddr args) `(cond ,(car args) ,(cadr args) (if ,@(cddr args))) `(cond ,(car args) ,(cadr args))) (car args)) nil))) (def map-helper-0 (f things lc) (loop (pair? things) ((fn nil (assign lc (cdr-set lc (cons (f (car things))))) (assign things (cdr things))))) (if things (cdr-set lc (f things)))) (def map-helper-1 (f things acc) (map-helper-0 f things acc) (cdr acc)) ;; transforms the list 'things by applying 'f to each item, returns the resulting list ;; conceptually, does the following: ;; ;; (if (pair? things) ;; (cons (f (car things)) (map f (cdr things))) ;; things ;; (f things)) ;; ;; however the actual version is more complicated to allow for TCO ("modulo-cons" issue) (def map (f things) (map-helper-1 f things (cons))) ;; push 'v onto the value for 'k in 'h ;; the hash-values of h will all be lists, in reverse order of consing (def hash-cons (h k v) (hash-set h k (cons v (hash-get h k)))) ;; 'things - the list to be reversed ;; 'last-cdr - (normally nil) - an item (atom, list, nil, anything) to be consed to the end of the reversed list. (def rev (things last-cdr) (loop (pair? things) ((fn nil (assign last-cdr (cons (car things) last-cdr)) (assign things (cdr things))))) last-cdr)