; The following sample code mostly comes from the Lisp Primer tutorial ; found at mypage.iu.edu/~colallen/lp/ and is taken with permission. (+ 1 2 3 4) (setq my-age 9) my-age (setq my-age 10) my-age 9 10 my-age t nil () (+ 2 13 45) (+ my-age 1) (cons 1 nil) (cons 1 (cons 2 nil)) 'a (cons 'a '(b c d)) (list 2 3 4) (list 'a '(a s d f)) (append '(a b) '(c d)) (first '(a s d f)) (first '((a s) d f)) (rest '(a s d f)) (rest '((a s) d f)) (rest '((a s) (d f))) (setq a '(a s d f)) (first a) (rest a) (first (rest a)) (cons 'a a) a (setq a (cons 'a a)) a (length '(1 2 3)) (length a) (length (append a a)) (length '(append a a)) (length (list a a)) (atom 'a) (atom a) (listp 'a) (listp a) (atom nil) (listp nil) (setq my-age (+ my-age 1)) (setq a (cdr a)) (setq words '(a list of words)) (setq words (cons 'this (rest words))) (first '(((a)) (b c d e))) (rest '(((((f)))))) (first '(rest (a b c))) (first '(rest (rest (a b c)))) (cons '(my life as) '(a dog)) (append '(my life as) '(a dog)) (list '(my life as) '(a dog)) (cons (rest nil) (first nil)) (abs (- (length (rest '(((a b) (c d))))) 5)) (setf trek '(picard riker laforge worf)) (cons 'data trek) trek (length (cons 'troi trek)) (setf trek (cons 'data trek)) (length (cons 'troi trek) ) (defun square (x) (* x x)) (square 2) (setf x 3) (square 345) x (defun y-plus (x) (+ x y)) (setq y 2) (y-plus 4) (setq x 5) (y-plus 23) x (setq y 27) (y-plus 43) (defun fourth-power (x) (square (square x))) (fourth-power 2) (defun powers-of (x) (square x) (fourth-power x)) (powers-of 2) (defun powers-of (x) (setq y (square x)) (fourth-power x)) y (powers-of 7) y (if (symbolp foo) (* 5 5) (+ 5 5)) (if (symbolp 1) (* 5 5) (+ 5 5)) (if t 'test-was-true 'test-was-false) (if nil 'test-was-true 'test-was-false) (defun absdiff (x y) (if (> x y) (- x y) (- y x))) (absdiff 10 3) (absdiff 3 10) (defun absdiff (x y) 1000) (absdiff 3 4) (defun absdiff (x y) (cond ((> x y) (- x y)) (t (- y x)))) (absdiff 3 10) (absdiff 10 3) (= 3 3.0) (= 3/1 6/2) (equal 3 3) (equal 3 3.0) (setf a '(1 2 3 4)) (setf b '(1 2 3 4)) (setf c b) (equal a b) (equal b c) (= (first a) (first b)) (null nil) (not nil) (null ()) (not ()) (null '( a s)) (not '(a s)) (not (= 1 (* 1 1))) (null (= 1 (* 1 1))) (and 1 2 3 4) (and 1 (cons 'a '(b)) (rest '(a)) (setf y 'hello)) y (or nil nil 2 (setf y 'goodbye)) (or (rest '(a)) (equal 3 4)) (defun life (a b) (cond ((null a) b) ((null b) a) (t 'its-tough))) (setf a 'oh-boy) (life 'gummi a) (defun circulate (lst) (append (rest lst) (list (first lst)))) (circulate '((whats) happening here)) (circulate nil) (defun power (x y) (if (= y 0) 1 (* x (power x (1- y))))) (power 3 4) (defun num-sublists (lis) (cond ((null lis) 0) ((listp (first lis)) (1+ (num-sublists (rest lis)))) (t (num-sublists (rest lis))))) (num-sublists '(a b c)) (num-sublists '(a (b c))) (num-sublists '(a (b c) q (4 5))) (let ((x 3) (y (- 67 34))) (* x y)) a b (let ((a 3) (b a))) b (let ((a 3) (b a)) b) b a (let* ((a 3) (b a)) b) b (defun mymax (nums) (cond ((= (length nums) 1) (car nums)) ((> (car nums) (cadr nums)) (mymax (cons (car nums) (cddr nums)))) (t (mymax (cdr nums))))) (mymax '(1 3 412 43 1 1 3412 53 43 43 54)) (defun palindromep (lst) (equal lst (reverse lst)) ) (palindromep '(1 2 3 4 5 4 3 2 1)) (palindromep '(a b b a)) (palindromep '(1 2 3)) (defun remove (lst elt) (cond ((null lst) nil) ((equal (first lst) elt) (rest lst)) (t (cons (first lst) (remove (rest lst) elt))))) (remove '(a 1 c 2 c 7) 'c) (remove '(a (1 q) 2) 'q) (defun mylength (lst) (cond ((null lst) 0) (t (+ 1 (mylength (rest lst)))))) (mylength nil) (mylength '(a b)) (mylength '(a b c d e f g)) (mylength '(a (2 q) 64)) (defun search (lst elt) (cond ((null lst) nil) ((atom (first lst)) (if (equal (first lst) elt) 't (search (rest lst) elt))) (t (or (search (first lst) elt) (search (rest lst) elt))))) (search '(a (1 c) 2 7) 'c) (search '(a (1 c) 2 7) 'q) (defun sum-of-list (lst) (cond ((null lst) 0) (t (+ (first lst) (sum-of-list (rest lst)))))) (defun product-of-list (lst) (cond ((null lst) 1) (t (* (first lst) (product-of-list (rest lst)))))) (sum-of-list '(2 3 4 5)) (product-of-list '(2 3 4 5)) (defun factorial (n) (cond ((= n 0) 1) (t (* n (factorial (- n 1)))))) (factorial 5) (defun remainder (n m) (cond ((< n m) n) (t (remainder (- n m) m)))) (remainder 30 7) (defun my-exp (x y) (cond ((= y 0) 1) (t (* x (my-exp x (- y 1)))))) (my-exp 3 4) (defun cube-list (lst) (cond ((null lst) nil) (t (cons (* (first lst) (first lst) (first lst)) (cube-list (rest lst)))))) (cube-list '(5 3 -15)) (defun cubeB (lst) (cond ((null lst) nil) (t (let ((elt (first lst))) (cons (* elt elt elt) (cubeB (rest lst))))))) (cubeB '(-15 5 3)) (defun cube (elt) (* elt elt elt)) (defun cubeC (lst) (cond ((null lst) nil) (t (cons (cube (first lst)) (cubeC (rest lst)))))) (cubec '(3 5 -15)) "This is a string" (cons "this" '(here)) (print 'this) (print (+ 1 2)) (+ (print 1) (print 2)) `(2 ,(+ 3 4)) `(4 ,(+ 1 2) '(3 ,(+ 1 2)) 5) (defmacro 2plus (x) (+ x 2)) (2plus 3) (setf a (2plus 3)) a (defmacro just-first-macro (x y) x) (just-first-macro 3 4) (just-first-macro 3 (print 4)) (defun just-first-function (x y) x) (just-first-function 3 (print 4)) (* 4 5 6) (* 123.5 12/3) (* (* 2 3) 8) (* 3) (*) (+ 4 5 6) (+ 123.5 12/3) (+ (* 2 3) 8) (+ 3) (+) (- 1) (- 1 2) (- 1 2 3) (- 1 2 3 4) (1+ 3) (1+ (1- 3)) (= 2) (= 2 3) (= 2 3 4) (= 2 2 2 2 ) (= 2 2 3 2) (< 2 67) (< 67 2) (> 67 2) (< 3 6 9) (< 4) (< 2 2 6) (<= 2 2 6) (>= 2 2 6) (< 6/2 3.0) (and 3 (+ 4 5)) (and nil (print 'hello)) (and t (print 'hello) 3) (and) (append '(a b) '(c d)) (append '(1 (2 (3))) '(i (j) k)) (setq tmp '(fee fi fo)) (append tmp (list 'fum)) (atom 'hello) (atom "hello") (atom 4.6434) (atom '(hello "hello" 4.6434)) (butlast '(a s d f)) (butlast '(a s d f) 2) (butlast '(a s d f) 0) (reverse (nthcdr 2 (reverse '(a s d f)))) (setf ds9 '(Sisko Kira Dax Odo Bashir Obrien)) (cadr ds9) (cddr ds9) (setq a '3) (setq b '4) (cond ((= a b) 'equal) ((< a b) 'b-is-bigger) (t 'a-is-bigger)) (setq action 'square-both) (cond ((equal action 'clear-both) (setq a 0) (setq b 0)) ((equal action 'square-both) (setq a (* a a)) (setq b (* b b))) ((equal action 'square-a) (setq a (* a a))) ((equal action 'square-b) (setq b (* b b)))) (cond ((= a 5) 'found) ((= b 5) 'found)) (cons 'a '(1 2 3)) (cons '(a) '(1 2 3)) (setq a 3) (cons a (list 'i 'j 'k)) (defun square (x) (* x x)) (square 4) (square (+ 7 -2)) (defun equal-length (lst1 lst2) (cond ((= (length lst1) (length lst2)) t) (t nil))) (equal-length '(a b c) '((d e) f g)) (equal-length '() (rest '(a b))) (defun side-effect (x y) (setq x (* x x x)) (+ x y)) (side-effect 2 3) (side-effect -4 14) (equal 'hey 'hello) (equal -81 -81) (equal '(1 (2 3)) '(1 (2 3))) (setq a '(1 2 3)) (setq b '(1 2 3)) (equal a b) (setq c a) (equal a c) (eval '(+ 1 2)) (eval (cons '+ '(1 2))) (eval 3) (eval (+ 3 4)) (first '(1 2 3)) (first '((a (b (c)) d) e (f))) (first ()) (if (> 4 3) 4 3) (if (< 4 3) (- 4 3) (- 3 4)) (if (= 4 3) t) (if (= 4 (+ 3 1)) t) (length '(1 2 3 4 5)) (length "1 2 3 4 5") (length nil) (let ((a) (b)) (and (not a) (not b))) (let ((a 3) (b 4)) (setf a (+ a b)) (setf b (+ a b)) (+ a b)) (list 'picard 'riker 'worf 'crusher) (list 'picard '(riker worf crusher)) (list 1 (+ 1 1) (+ 1 1 1) (+ 1 1 1 1)) (listp '(a s d f)) (listp 3) (listp (cons '1 '(2 3 4))) (nth 0 '(picard riker work crush)) (nth 2 '(( captain picard) (commander riker) (lieutenant worf) (ensign crusher))) (setf ds9 '(Sisko Kira Dax Odo Bashir Obrien) ) (nthcdr 0 ds9) (nthcdr 1 ds9) (nthcdr 3 ds9) (nthcdr 2345 ds9) (null '(picard riker)) (null (rest '(picard))) (or 3 (+ 4 5)) (or nil (print 'hello)) (or nil '(print hello) 3) (or) (rest '(1 2 3)) (rest '((a (b (c)) d) e (f))) (rest '(r)) (rest ()) (reverse '(picard riker worf crusher)) (reverse (reverse '(picard riker worf crusher))) (reverse '((this list) (of words))) (second '(1 2 3 4)) (fourth '(1 2 3 4))