;; take the length of a list in a structurally recursive way (defun len (list) (cond ((atom list) 0) (T (+ (len (cdr list)) 1)) ) ) (len '(a b)) (len '(a (b c) d e)) (len NIL) (len 'a) ;; get the first n elements from a list (defun take (list n) (cond ((null list) nil) ((zerop n) nil) (T (cons (car list) (take (cdr list) (- n 1)))) ) ) (take '(a b c d e f) 0) (take '(a b c d e f) 1) (take '(a b c d e f) 2) (take '(a b c d e f) 3) (take '(a b) 30) (take NIL 3) ;; drop the first n elements from a list (defun drop (list n) (cond ((null list) nil) ((zerop n) list) (T (drop (cdr list) (- n 1))) ) ) (drop '(a b c d e f) 3) (drop '(a b c d e f) 0) (drop '(a b) 30) (drop NIL 3) ;; insert an element at position i (defun insert (list elem i) (cond ((< i 1) (cons elem list)) ( T (append (take list (- i 1)) (list elem) (drop list (- i 1)))) ) ) (setq z '(ant boa cow dog elk fox gnu hog)) (insert z 'human 0) (insert z 'human 1) (insert z 'human 2) (insert z 'human 3) (insert z 'human (length z)) (insert z 'human (+ (length z) 1)) (insert z 'human (+ (length z) 2)) ;; increment all the numbers in a list (defun incit (z) (mapcar #'(lambda (x) (cond ((numberp x) (+ x 1)) ((atom x) x) (T (incit x)) )) z ) ) (incit '(x)) (incit '(4 r 6)) (incit '(((665) turtle (swim 998)))) (incit '(x 3 y 76.4)) (incit 3) (incit '(4)) (incit '(a b 3 (f (3) 3) ((3) ((3)))))) (incit (incit (incit '(a b 3 (f (3) 3) ((3) ((3))))))) (defun nestlist (func x n) (cond ((= n 0) (list x)) ( t (cons x (nestlist func (funcall func x) (- n 1)))) ) ) (defun nestlist (func x n) (cond ((= n 0) (list x)) ( t (cons x (mapcar func (nestlist func x (- n 1))))) ) ) (nestlist 'list 'a 0) (nestlist 'list 'a 1) (nestlist 'list 'a 2) (nestlist 'list 'a 3) (nestlist 'car '((a) b) 2) (nestlist 'cdr '((a) b) 2) (nestlist 'cdr '((a) b) 5) ;; do a matrix transpose where a matrix is a list of lists representing rows (defun transpose (x) (cond ((null (car x)) nil) (T (cons (mapcar 'car x) (transpose (mapcar 'cdr x)))) ) ) (transpose '(nil nil nil)) (transpose '((a) (b) (c))) (transpose '((a) (b))) (transpose '((a))) (transpose '((a b c) (p q r) (x y z))) (transpose '((a b c) (p q r))) (transpose '((a b c)) (defun mapsym (z mapping) (cond ((atom z) (if (assoc z mapping) (second (assoc z mapping)) z ) ) ( T (mapcar #'(lambda (x) (mapsym x mapping)) z) ) )) (defun mapsym (z mapping) (cond ((atom z) (cond ((second (assoc z mapping))) (z) ) ) ( T (mapcar #'(lambda (x) (mapsym x mapping)) z) ) )) (defun mapsym (z mapping) (cond ((atom z) (or (second (assoc z mapping)) z) ) ( T (mapcar #'(lambda (x) (mapsym x mapping)) z) ) )) (defun mapsym-toponly (z mapping) (cond ((atom z) (or (second (assoc z mapping)) z) ) ( T (mapcar #'(lambda (x) (or (second (assoc x mapping)) x)) z) ) )) (mapsym 'a '((a w) (b x) (c y) (d z))) ;; W (mapsym 'z '((a w) (b x) (c y) (d z))) ;; Z (mapsym NIL '((a w) (b x) (c y) (d z))) ;; NIL (mapsym '(a b) '((a c))) ;; (C B) (mapsym '(a b c z) '((a b) (b c) (c a))) ;; (B C A Z) (mapsym '(a ((b) m ((c)) ()) d) '((a w) (b x) (c y) (d z))) ;; (W ((X) M ((Y)) NIL) Z) (mapsym '(a ((b) (m (c)) ()) d) '((a b) (b c) (c d) (d e))) ;; (B ((C) (M (D)) NIL) E) (mapsym '(a ((b) ((c) m) ()) d) '((b a) (c b) (d c) (e d))) ;; (A ((A) ((B) M) NIL) C) (mapsym '(((((a)))) NIL) '((a z))) ;; (((((Z)))) NIL) ;; reverse all the lists in an alist (defun assocswap (alist) (mapcar '(lambda (x) (reverse x)) alist) ) (assocswap '((a b) (x y) (u v))) ;; do a mapcar with union instead of list (defun mapunion (f l) (reduce 'union (mapcar f l)) ) (defun mapall (func list) (cond ((listp list) (mapcar '(lambda (x) (mapall func x)) list)) ( T (funcall func list)) ) ) (mapall 'list '(a (b) ((c) d)))