;;;******************************************** ;;; Lisp Syntax Notes ;;;******************************************** ;;;******************************************** ;;; List constructors: (list 'a '(b) 'c) => (A (B) C) (cons 'a '(b c)) => (A B C) (append '(a b) '(c d)) => (A B C D) ;;;******************************************** ;;; List operators: ;; car and first are interchangeable (car '((1 2) (3 4) (5 6))) => (1 2) ;; cdr and rest are interchangeable (cdr '((1 2) (3 4) (5 6))) => ((3 4) (5 6)) (length '(1 (3) a b)) => 4 ;;;******************************************** ;;; Syntax of conditionals: (defun sign (x) (cond ((< x 0) -1) ((= x 0) 0) (t 1))) (defun print-fuel-level (level) (case level ((FULL MEDIUM) (format t "~%Level is fine.~%")) ((LOW) (format t "~%Refuel soon!~%")) ((EMPTY) (format t "~%You are out of gas!!~%")) (OTHERWISE (error "Bad input symbol, ~a" level)))) (defun warn-if-hot (temp) (when (> temp 100) (format t "~%Careful, temperature ~a is hot.~%" temp))) (defun abs-value (x) (if (>= x 0) x (- x))) ;;;**************************************************** ;;; Mapping constructs (mapcar #'(lambda (x) (* x 2)) '(2 1 3)) => (4 2 6) (mapcar #'symbolp '(2 a b c)) => (NIL T T T) (some #'symbolp '(2 a b c)) => T (every #'symbolp '(2 a b c)) => NIL (mapc #'(lambda (temp) (warn-if-hot temp)) '(-10 200 40 300)) => returns nothing important, but prints messages: => Careful, temperature 200 is hot. => => Careful, temperature 300 is hot. (remove-if #'symbolp '(2 a b c)) => (2) (remove-if-not #'symbolp '(2 a b c)) => (A B C) (find 'a '(2 a b c)) => A (find-if #'(lambda (x) (and (numberp x) (> x 0))) '(-2 a 3 b c)) => 3 (member 'b '(2 a b c)) => '(B C) (position 3.2 '(1 3 3.2 4) :test '=) => 2 (position 3.3 '(1 3 3.2 4) :test '=) => NIL (position-if #'(lambda (x) (and (numberp x) (> x 0))) '(-2 a 3 b c)) => 2 (dolist (x '(2 a b c)) (when (symbolp x) (format t "~%~a is a symbol" x))) => A is a symbol => B is a symbol => C is a symbol => NIL ;;;******************************************************* ;;; Association lists: (setf *alist* '((RASPBERRY RED) (LEMON YELLOW) (ORANGE ORANGE))) (assoc 'raspberry *alist*) => (RASPBERRY RED) (assoc 'green *alist*) => NIL