start of list all lisp examples - karlstad university · 2013-05-18 · *** start of list all lisp...

58
*** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;; ;;===================================================================== ;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993] ;; Chapter 2 pp 10-11 ;;===================================================================== ;; ;; ;;===================================================================== ;; Introductory examples in LISP 1 ;;===================================================================== ;; (+ 2 2) 4 * ;; (setf friends '(kalle nisse lisa)) Warning: Declaring FRIENDS special. (KALLE NISSE LISA) * friends (KALLE NISSE LISA) * ;; (setf enemies '(troll cobol ghost)) Warning: Declaring ENEMIES special. (TROLL COBOL GHOST) * enemies (TROLL COBOL GHOST) * (setf enemies (remove 'ghost enemies)) (TROLL COBOL) * ;; (setf friends (cons 'ghost friends)) (GHOST KALLE NISSE LISA) * ;; (setf friends '(kalle nisse lisa)) (KALLE NISSE LISA) * (setf enemies '(troll cobol ghost)) (TROLL COBOL GHOST) * ;; (defun newfriend (name) (setf enemies (remove name enemies)) (setf friends (cons name friends)) ) NEWFRIEND * (newfriend 'ghost) (GHOST KALLE NISSE LISA) * friends (GHOST KALLE NISSE LISA) * enemies

Upload: others

Post on 23-Jan-2020

37 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

*** Start of list all Lisp examples ***

*** listing lisp_1 ***

*** listing lisp_1/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 10-11;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 1;;=====================================================================;;(+ 2 2)

4* ;;(setf friends '(kalle nisse lisa))Warning: Declaring FRIENDS special.

(KALLE NISSE LISA)* friends

(KALLE NISSE LISA)* ;;(setf enemies '(troll cobol ghost))Warning: Declaring ENEMIES special.

(TROLL COBOL GHOST)* enemies

(TROLL COBOL GHOST)* (setf enemies (remove 'ghost enemies))

(TROLL COBOL)* ;;(setf friends (cons 'ghost friends))

(GHOST KALLE NISSE LISA)* ;;(setf friends '(kalle nisse lisa))

(KALLE NISSE LISA)* (setf enemies '(troll cobol ghost))

(TROLL COBOL GHOST)* ;;(defun newfriend (name) (setf enemies (remove name enemies)) (setf friends (cons name friends)) )

NEWFRIEND* (newfriend 'ghost)

(GHOST KALLE NISSE LISA)* friends

(GHOST KALLE NISSE LISA)* enemies

Page 2: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(TROLL COBOL)* ;;;;=====================================================================(dribble)

*** listing lisp_1/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 18-20;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 2;;=====================================================================;;'(a b c)

(A B C)* '(+ 2 2)

(+ 2 2)* (+ 2 2)

4* ;;;;=====================================================================;; example of primitive FIRST;;=====================================================================;;(first '(fast computers are nice))

FAST* (first () )

NIL* (first '((a b) (c )))

(A B)* ;;;;=====================================================================;; example of primitive REST;;=====================================================================;;(rest '(fast computers are nice))

(COMPUTERS ARE NICE)* (rest '(c))

NIL* (rest () )

NIL* ;;;;=====================================================================;;(first (rest '(a b c)))

B* (first '(rest (a b c)))

REST* ;;;;=====================================================================

Page 3: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;(dribble)

*** listing lisp_1/lisp3.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 22-24;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 3;;=====================================================================;;(setf ab-list '(a b))Warning: Declaring AB-LIST special.

(A B)* ab-list

(A B)* 'ab-list

AB-LIST* (first ab-list)

A* (rest ab-list)

(B)* ;;(setf ab-list '(a b) xy-list '(x y))Warning: Declaring XY-LIST special.

(X Y)* ab-list

(A B)* xy-list

(X Y)* ;;;;=====================================================================;; examples of literal symbols;;=====================================================================;;t

T* nil

NIL* 2

2* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp4.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993]

Page 4: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;; Chapter 2 pp 24-27;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 1;;=====================================================================;;=====================================================================;; examples of the primitive CONS;;=====================================================================;;(setf head 'a tail '(b c))Warning: Declaring HEAD special.Warning: Declaring TAIL special.

(B C)* (cons head tail)

(A B C)* (first (cons head tail))

A* (rest (cons head tail))

(B C)* (setf newlist (cons head tail))Warning: Declaring NEWLIST special.

(A B C)* (first newlist)

A* (rest newlist)

(B C)* (cons (first newlist) (rest newlist))

(A B C)* ;;;;=====================================================================;; examples of the primitive APPEND;;=====================================================================;;(setf ab-list '(a b) xy-list '(x y))Warning: Declaring AB-LIST special.Warning: Declaring XY-LIST special.

(X Y)* (append ab-list xy-list)

(A B X Y)* (append ab-list '() xy-list '() )

(A B X Y)* (append ab-list xy-list ab-list)

(A B X Y A B)* (append '( (a) (b) ) '( (c) (d) ))

((A) (B) (C) (D))* ;;;;=====================================================================;; examples of the primitive LIST;;=====================================================================;;(setf front 'a middle 'b back 'c)Warning: Declaring FRONT special.

Page 5: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

Warning: Declaring MIDDLE special.Warning: Declaring BACK special.

C* (list front middle back)

(A B C)* ;;(setf ab-list '(a b))

(A B)* (list ab-list ab-list)

((A B) (A B))* (list ab-list ab-list ab-list)

((A B) (A B) (A B))* (list 'ab-list ab-list)

(AB-LIST (A B))* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp5.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 p 26;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 5;;=====================================================================;;;;=====================================================================;; Differences between CONS, APPEND and LIST;;=====================================================================;;(setf ab-list '(a b) cd-list '(c d))Warning: Declaring AB-LIST special.Warning: Declaring CD-LIST special.

(C D)* (append ab-list cd-list)

(A B C D)* (list ab-list cd-list)

((A B) (C D))* (cons ab-list cd-list)

((A B) C D)* ;;(append ab-list ab-list)

(A B A B)* (list ab-list ab-list)

((A B) (A B))* (cons ab-list ab-list)

((A B) A B)* ;;

Page 6: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(list 'ab-list ab-list)

(AB-LIST (A B))* (cons 'ab-list ab-list)

(AB-LIST A B)* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp6.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 27-28;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 6;;=====================================================================;;;;=====================================================================;; examples of assigning lists;;=====================================================================;;(setf head 'a tail '(b c))Warning: Declaring HEAD special.Warning: Declaring TAIL special.

(B C)* (cons head tail)

(A B C)* head

A* tail

(B C)* ;;(setf head 'a tail '(b c))

(B C)* (setf tail (cons head tail))

(A B C)* head

A* tail

(A B C)* ;;;;=====================================================================;; examples of the primitives PUSH and POP;;=====================================================================;;(setf head 'a tail '(b c))

(B C)* (push head tail)

(A B C)* tail

Page 7: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(A B C)* (pop tail)

A* tail

(B C)* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp7.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 28-31;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 7;;=====================================================================;;;;=====================================================================;; example of the primitive NTHCDR;;=====================================================================;;(setf abc-list '(a b c))Warning: Declaring ABC-LIST special.

(A B C)* (rest abc-list)

(B C)* (nthcdr 2 abc-list)

(C)* ;;;;=====================================================================;; example of the primitive BUTLAST;;=====================================================================;;(setf abc-list '(a b c))

(A B C)* (butlast abc-list 2)

(A)* (butlast abc-list 50)

NIL* (butlast abc-list)

(A B)* ;;;;=====================================================================;; more examples of the primitives FIRST and LAST;; Introductory examples in LISP 7;;=====================================================================;;(setf abc-list '(a b c) ab-cd-list '((a b) (c d)))Warning: Declaring AB-CD-LIST special.

((A B) (C D))

Page 8: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* (last abc-list)

(C)* (last ab-cd-list)

((C D))* ;;(setf abc-list '(a b c))

(A B C)* (last abc-list)

(C)* (first (last abc-list))

C* ;;;;=====================================================================;; example of the primitive LENGTH;;=====================================================================;;(setf ab-list '(a b) ab-cd-list '((a b) (c d)))Warning: Declaring AB-LIST special.

((A B) (C D))* (length ab-list)

2* (length ab-cd-list)

2* (length (append ab-list ab-list))

4* ;;;;=====================================================================;; example of the primitive REVERSE;;=====================================================================;;(reverse ab-list)

(B A)* (reverse ab-cd-list)

((C D) (A B))* (reverse (append ab-list ab-list))

(B A B A)* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp8.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 31-32;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 8;;=====================================================================;;;;=====================================================================

Page 9: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;; example of an ASSOC list;;=====================================================================;;(setf sarah '((height .54) (weight 4.4)))Warning: Declaring SARAH special.

((HEIGHT 0.54) (WEIGHT 4.4))* (assoc 'weight sarah)

(WEIGHT 4.4)* ;;;;=====================================================================;;(dribble)

*** listing lisp_1/lisp9.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 2 pp 32-35;;=====================================================================;;;;;;=====================================================================;; Introductory examples in LISP 9;;=====================================================================;;;;=====================================================================;; example of interger and ratio result;;=====================================================================;;(/ 27 9)

3* (/ 22 7)

22/7* ;;;;=====================================================================;; example of the primitives FLOAT and ROUND;;=====================================================================;;(float (/ 22 7))

3.142857* (round (/ 22 7))

31/7* (+ (round (/ 22 7)) (round(/ 7 3)))

5* (round (/ 5 2))

21/2* ;;;;=====================================================================;; examples of mixed (integer & float) arithmetic;;=====================================================================;;(+ 2 1.5)

3.5* (+ (float 2) (float 1.5))

Page 10: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

3.5* ;;;;=====================================================================;; examples of unary - and /;;=====================================================================;;(- 8)

-8* (- -8)

8* (/ 1 2)

1/2* (/ 2)

1/2* ;;;;=====================================================================;; examples of the primitives MIN and MAX;;=====================================================================;;(max 2 4 3)

4* (min 2 4 3)

2* ;;;;=====================================================================;; example of the primitive EXPT (exponentiation);;=====================================================================;;(expt 2 3)

8* (expt 3 2)

9* (expt 3.3 2.2)

13.827086* (expt 2.2 3.3)

13.48947* ;;;;=====================================================================;; example of the primitive SQRT (square root);;=====================================================================;;(sqrt 9)

3.0* (sqrt -1)

#C(-4.3711388e-8 1.0)* ;;;;=====================================================================;; example of the primitive ABS (absolute value);;=====================================================================;;(abs 5)

5* (abs -5)

Page 11: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

5* ;;;;=====================================================================;;(dribble)

*** listing lisp_2 ***

*** listing lisp_2/lisp1.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 3 pp 37-42;;=====================================================================;;;;;;=====================================================================;; examples of procedure definition - DEFUN primitive;;=====================================================================;;(setf meals '(breakfast lunch tea dinner))Warning: Declaring MEALS special.

(BREAKFAST LUNCH TEA DINNER)* (cons (first meals) (last meals))

(BREAKFAST DINNER)* ;;;;(setf route2 '(boston cambridge lincoln concord))Warning: Declaring ROUTE2 special.

(BOSTON CAMBRIDGE LINCOLN CONCORD)* (cons (first route2) (last route2))

(BOSTON CONCORD)* ;;;;=====================================================================;; a procedure for the above operations;;=====================================================================;;(defun both-ends (whole-list) (cons (first whole-list) (last whole-list)) )

BOTH-ENDS* (both-ends meals)

(BREAKFAST DINNER)* (both-ends route2)

(BOSTON CONCORD)* ;;;;=====================================================================;; a more compact version of the procedure (meaningful names...???);;=====================================================================;;(defun b-e (w) (cons (first w) (last w)))

B-E* (b-e meals)

(BREAKFAST DINNER)* (b-e route2)

Page 12: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(BOSTON CONCORD)* ;;;;=====================================================================;; example of a procedure with two parameters;;=====================================================================;;(defun be3 (l m) (cons (first l) (last m)))

BE3* (be3 meals route2)

(BREAKFAST CONCORD)* ;;;;=====================================================================;;(dribble)

*** listing lisp_2/lisp2.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 3 pp 43-46;;=====================================================================;;;;;;=====================================================================;; examples of the primitives LET and LET* (scope rules in LISP);;=====================================================================;;;;=====================================================================;; example of LET primitive;;=====================================================================;;(setf wl '(breakfast lunch tea dinner))Warning: Declaring WL special.

(BREAKFAST LUNCH TEA DINNER)* (let ((el (first wl)) (tr (last wl)))(cons el tr))

(BREAKFAST DINNER)* ;;;;=====================================================================;; example of LET primitive illustrating lexical scope;;=====================================================================;;(setf x 'outside)Warning: Declaring X special.

OUTSIDE* (let ((x 'inside) (y x)) (list x y))

(INSIDE OUTSIDE)* ;;;;=====================================================================;; example of LET* primitive illustrating lexical scope;;=====================================================================;;(setf x' outside)

OUTSIDE* (let* ((x 'inside) (y x)) (list x y))

Page 13: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(INSIDE INSIDE)* ;;;;=====================================================================;; equivalent nesting of LET primitives to the above LET* example;;=====================================================================;;(setf x 'outside)

OUTSIDE* (let ((x 'inside)) (let ((y x)) (list x y)))

(INSIDE INSIDE)* ;;;;=====================================================================;;(dribble)

*** listing lisp_3 ***

*** listing lisp_3/lisp1.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 50-53;;=====================================================================;;;;;;=====================================================================;; examples of comparison with equal;;=====================================================================(equal (+ 2 2) 4)

T* (equal (+ 2 2) 3)

NIL* (equal '(a list) (setf l '(a list)))Warning: Declaring L special.

T* (equal '(a list) l)

T* (equal '(a list) (setf rev-l '(list a)))Warning: Declaring REV-L special.

NIL* (equal l (reverse rev-l))

T* ;;=====================================================================;; examples of comparison with eql & =;;=====================================================================(eql 4 4.0)

NIL* (eql 4 4)

T* (= 4 4.0)

T

Page 14: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;=====================================================================;; examples of the member primitive;;=====================================================================(setf query '(tell me about your mother please))Warning: Declaring QUERY special.

(TELL ME ABOUT YOUR MOTHER PLEASE)* (member 'mother query)

(MOTHER PLEASE)* ;;(setf pairs '((father son) (mother daughter)))Warning: Declaring PAIRS special.

((FATHER SON) (MOTHER DAUGHTER))* (member 'mother pairs)

NIL* ;;(setf pairs '((maple shade) (apple fruit)))

((MAPLE SHADE) (APPLE FRUIT))* (member '(maple shade) pairs)

NIL* (member '(maple shade) pairs :test #'equal)

((MAPLE SHADE) (APPLE FRUIT))* ;;(setf predicate #'equal)Warning: Declaring PREDICATE special.

#<Function EQUAL {101A249}>* (member '(maple shade) pairs :test predicate)

((MAPLE SHADE) (APPLE FRUIT))* ;;(setf pairs '((maple shade) (apple fruit)))

((MAPLE SHADE) (APPLE FRUIT))* (member '(maple shade) pairs :test-not #'equal)

((APPLE FRUIT))* ;;(member '(maple shade) '((maple shade) (maple shade)) :test-not #'equal)

NIL* ;;=====================================================================;;(dribble)

*** listing lisp_3/lisp2.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 53-54;;=====================================================================;;;;;;=====================================================================;; examples of predicates;;=====================================================================;;;;=====================================================================;; examples of the predicate ATOM - is an object an atom?

Page 15: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;=====================================================================;;(atom 'pi)

T* (atom pi)

T* ;;;;=====================================================================;; examples of the predicate NUMBERP - is an object a number?;;=====================================================================;;(numberp 'pi)

NIL* (numberp pi)

T* ;;;;=====================================================================;; examples of the predicate SYMBOLP - is an object a symbol?;;=====================================================================;;(symbolp 'pi)

T* (symbolp pi)

NIL* ;;;;=====================================================================;; examples of the predicate LISTP - is an object a list?;;=====================================================================;;(listp 'pi)

NIL* (listp pi)

NIL* (listp '(pi list))

T* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp3.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 p 55;;=====================================================================;;;;;;=====================================================================;; NIL and the empty list () - these are equivalent in LISP;;=====================================================================;;;;=====================================================================;; equality tests with NIL and ();;=====================================================================;;

Page 16: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(eq NIL '())

T* (eql NIL '())

T* (equal NIL '())

T* ;;;;=====================================================================;; printed values for NIL and () - by convention always NIL;;=====================================================================;;nil

NIL* ()

NIL* ;;;;=====================================================================;; NIL and () are BOTH symbols and lists !!!;;=====================================================================;;(atom nil)

T* (atom ())

T* (symbolp nil)

T* (symbolp ())

T* (listp nil)

T* (listp ())

T* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp4.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 p 56;;=====================================================================;;;;;;=====================================================================;; example of list predicates;; NULL - is the arg an empty list?;; ENDP - is the arg (MUST BE A LIST) an empty list?;;=====================================================================;;;;(null '(this is not empty))

Page 17: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

NIL* (endp '(this is not empty))

NIL* ;;(null ())

T* (endp ())

T* ;;(null 'a-symbol)

NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp5.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 56-58;;=====================================================================;;;;;;=====================================================================;; example of number predicates;;;; numberp is the arg a number?;; zerop is the arg zero?;; plusp is the arg positive?;; minusp is the arg negative?;; evenp is the arg even?;; oddp is the arg odd?;; > are the args in descending order?;; < are the args in ascending order?;;=====================================================================;;(setf zero 0 one 1 two 2 three 3 four 4)Warning: Declaring ZERO special.Warning: Declaring ONE special.Warning: Declaring TWO special.Warning: Declaring THREE special.Warning: Declaring FOUR special.

4* (setf digits (list zero one two three four))Warning: Declaring DIGITS special.

(0 1 2 3 4)* ;;(numberp 4)

T* (numberp four)

T* (numberp 'four)

NIL* (numberp digits)

Page 18: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

NIL* (numberp 'digits)

NIL* ;;(zerop zero)

T* (zerop four)

NIL* ;;(plusp one)

T* (plusp (- one))

NIL* (plusp zero)

NIL* ;;(evenp (* 9 7 5 3 1))

NIL* (evenp (* 10 8 6 4 2))

T* ;;(> four two)

T* (> two four)

NIL* (> three two one)

T* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp6.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 60-61;;=====================================================================;;;;;;=====================================================================;; examples of AND and OR;;=====================================================================;;(setf pets '(dog cat))Warning: Declaring PETS special.

(DOG CAT)* ;;(and (member 'dog pets) (member 'tiger pets))

NIL* (and (member 'dingo pets) (member 'tiger pets))

Page 19: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

NIL* (and (member 'dog pets) (member 'cat pets))

(CAT)* ;;(or (member 'dog pets) (member 'tiger pets))

(DOG CAT)* ;;;;=====================================================================;; examples of NOT;;=====================================================================;;(not nil)

T* (not T)

NIL* (not 'dog)

NIL* ;;(member 'dog pets)

(DOG CAT)* (not (member 'dog pets))

NIL* (member 'dingo pets)

NIL* (not (member 'dingo pets))

T* ;;(and (member 'dog pets) (member 'tiger pets))

NIL* (and (member 'dog pets) (not (member 'tiger pets)))

T* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp7.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 61-63;;=====================================================================;;;;;;=====================================================================;; examples of IF, WHEN and UNLESS;;=====================================================================;;(setf day-or-date 'monday)Warning: Declaring DAY-OR-DATE special.

MONDAY* (if (symbolp day-or-date) 'day 'date)

Page 20: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

DAY* (setf day-or-date 9)

9* (if (symbolp day-or-date) 'day 'date)

DATE* ;;(setf high 98 temp 102)Warning: Declaring HIGH special.Warning: Declaring TEMP special.

102* (when (> temp high) (setf high temp) 'new-record)

NEW-RECORD* ;;(setf high 98 temp 102)

102* (unless (> temp high) (setf high temp) 'new-record)

NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp8.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 63-65;;=====================================================================;;;;;;=====================================================================;; examples of COND;;=====================================================================;;(setf object 'sphere r 1)Warning: Declaring OBJECT special.Warning: Declaring R special.

1* (cond ((eq object 'circle) (* pi r r)) ((eq object 'sphere) (* 4 pi r r)) )

12.566370614359172d0* ;;(setf object 'sphere r 1)

1* (cond ((eq object 'circle) (* pi r r)) (t (* 4 pi r r)) )

12.566370614359172d0* ;;(setf object 'sphere r 1)

1* (cond ((eq object 'circle) (* pi r r)) ((* 4 pi r r))

Page 21: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

)

12.566370614359172d0* ;;(setf p .6)Warning: Declaring P special.

0.6* (cond ((> p .75) 'very-likely) ((> p .5) 'likely) ((> p .25) 'unlikely) (t 'very-unlikely) )

LIKELY* ;;(setf breakfast '(eggs bacon toast tea))Warning: Declaring BREAKFAST special.

(EGGS BACON TOAST TEA)* (cond ((> (length breakfast) 10) 'glutton) ((not (endp breakfast)) 'normal) (t 'anorexic) )

NORMAL* ;;(setf breakfast '(eggs bacon toast tea))

(EGGS BACON TOAST TEA)* (cond ((> (length breakfast) 10) 'glutton) (breakfast 'normal) (t 'anorexic) )

NORMAL* ;;;;=====================================================================;;(dribble)

*** listing lisp_3/lisp9.txt ***

*;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 4 pp 65-66;;=====================================================================;;;;;;=====================================================================;; examples of CASE;;=====================================================================;;;;=====================================================================;; example COND expressed as a CASE;;=====================================================================;;(setf object 'sphere r 1)Warning: Declaring OBJECT special.Warning: Declaring R special.

1* (cond ((eq object 'circle) (* pi r r)) ((eq object 'sphere) (* 4 pi r r)) )

Page 22: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

12.566370614359172d0* ;;(case object (circle (* pi r r)) (sphere (* 4 pi r r)) )

12.566370614359172d0* ;;(setf object 'point r 1)

1* (case object (circle (* pi r r)) (sphere (* 4 pi r r)) (otherwise 0) )

0* ;;(setf object 'ball r 1)

1* (case object ((circle wheel) (* pi r r)) ((sphere ball) (* 4 pi r r)) (otherwise 0) )

12.566370614359172d0* ;;;;=====================================================================;;(dribble)

*** listing lisp_4 ***

*** listing lisp_4/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 69-71;;=====================================================================;;;;;;=====================================================================;; Procedure abstraction examples;;=====================================================================;;(defun both-ends (w) (cons (first w) (last w)))

BOTH-ENDS* (both-ends '(this is a list example))

(THIS EXAMPLE)* ;;;;;;=====================================================================;; Alternative version of the above procedure (both-ends);;=====================================================================;;(defun combine (e1 e2) (list e1 e2))

COMBINE* (defun get-first-el (L) (first L))

Page 23: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

GET-FIRST-EL* (defun get-last-el (L) (first (last L)))

GET-LAST-EL* ;;(defun both-ends-2 (L) (combine (get-first-el L) (get-last-el L)))

BOTH-ENDS-2* (both-ends-2 '(this is a list example))

(THIS EXAMPLE)* ;;;;;;=====================================================================;; Alternative version of the above procedure (both-ends);;=====================================================================;;(defun combine (e1 e2) (cons e1 (cons e2 nil)))

COMBINE* (defun get-first-el (L) (car L))

GET-FIRST-EL* (defun get-last-el (L) (first (reverse L)))

GET-LAST-EL* ;;(defun both-ends-3 (L) (combine (get-first-el L) (get-last-el L)))

BOTH-ENDS-3* (both-ends-3 '(this is a list example))

(THIS EXAMPLE)* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 71-74;;=====================================================================;;;;;;=====================================================================;; recursive procedure examples;;=====================================================================;;;;=====================================================================;; singly recursive procedure;;=====================================================================;;(defun recursive-expt (m n) (if (zerop n) 1 (* m (recursive-expt m (- n 1))) ) )

RECURSIVE-EXPT* ;;(trace recursive-expt)

Page 24: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(RECURSIVE-EXPT)* ;;(recursive-expt 2 3)8* ;;;;=====================================================================;; doubly recursive procedure;;=====================================================================;;(defun fibonacci (n) (if (or (= n 0) (= n 1)) 1 (+ (fibonacci (- n 1)) (fibonacci (- n 2))) ) )

FIBONACCI* ;;(trace fibonacci)

(FIBONACCI)* ;;(fibonacci 4)5* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp3.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 75-78;;=====================================================================;;;;;;=====================================================================;; tail recursive procedure examples;;=====================================================================;;(defun count-elems (L) (if (endp L) 0 (+ 1 (count-elems (rest L))) ) )

COUNT-ELEMS* ;;(trace count-elems)

(COUNT-ELEMS)* ;;(count-elems '(fast computers are nice))4* ;;(untrace)

T* ;;;;=====================================================================;; tail recursive procedure;;=====================================================================;;

Page 25: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(defun count-elems-aux (L result) (if (endp L) result (count-elems-aux (rest L) (+ 1 result)) ) )

COUNT-ELEMS-AUX* ;;(defun count-elems (L) (count-elems-aux L 0) )

COUNT-ELEMS* ;;(trace count-elems-aux count-elems)

(COUNT-ELEMS-AUX COUNT-ELEMS)* ;;(count-elems '(fast computers are nice))4* ;;(untrace)

T* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp4.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 79-81;;=====================================================================;;;;;;=====================================================================;; doubly recursive procedure example;;=====================================================================;;(defun count-atoms (x) (cond ((null x) 0) ; x is empty list? ((atom x) 1) ; x is an atom? ; x must be a list (t (+ (count-atoms (first x)) (count-atoms (rest x)))) ) )

COUNT-ATOMS* ;;(trace count-atoms)

(COUNT-ATOMS)* ;;(count-atoms '(sqrt (expt x 2) (expt y 2)))7* ;;(untrace)

T* ;;;;=====================================================================(dribble)

Page 26: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

*** listing lisp_4/lisp5.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 83-85;;=====================================================================;;;;;;=====================================================================;; optional parameters in procedures;;=====================================================================;;(defun root ( x &optional n) (if n (expt x (/ 1 n)) (sqrt x) ) )

ROOT* ;;(root 9)

3.0* ;;(root 27 3)

3.0* ;;;;=====================================================================;; optional parameters in procedures - with default value;;=====================================================================;;(defun root-2 ( x &optional (n 2)) (expt x (/ 1 n)) )

ROOT-2* ;;(root-2 9)

3.0* ;;(root-2 27 3)

3.0* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp6.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 p 85;;=====================================================================;;;;;;=====================================================================;; optional parameter example for count elements example;;=====================================================================;;

Page 27: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(defun count-elem-opt (L &optional (result 0)) (if (endp L) result (count-elem-opt (rest L) (+ 1 result)) ) )

COUNT-ELEM-OPT* ;;(trace count-elem-opt)

(COUNT-ELEM-OPT)* ;;(count-elem-opt '(fast computers are nice))4* ;;(untrace)

T* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp7.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 87-88;;=====================================================================;;;;;;=====================================================================;; example of procedure with &REST parameter;;=====================================================================;;(defun raise-aux (result number-list) (if (endp number-list) result (raise-aux (expt result (first number-list)) (rest number-list)) ) )

RAISE-AUX* ;;(defun raise ( x &REST numbers) (raise-aux x numbers) )

RAISE* ;;(trace raise raise-aux)

(RAISE RAISE-AUX)* ;;(raise 2)2* ;;(raise 2 3)8* ;;(raise 2 3 5)32768* ;;(untrace)

Page 28: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

T* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp8.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 88-89;;=====================================================================;;;;;;=====================================================================;; example of procedure with &KEY parameter;;=====================================================================;;(defun rotate-list-right (L n) (if (zerop n) L (rotate-list-right (append (last L) (butlast L)) (- n 1)) ) )

ROTATE-LIST-RIGHT* ;;(defun rotate-list-left (L n) (if (zerop n) L (rotate-list-left (append (rest L) (list (first L))) (- n 1)) ) )

ROTATE-LIST-LEFT* ;;;;=====================================================================;; rotate-list version 1;;=====================================================================;;(defun rotate-list (L &KEY direction distance) (if (eq direction 'left) (rotate-list-left L (if distance distance 1)) (rotate-list-right L (if distance distance 1)) ) )

ROTATE-LIST* ;;(trace rotate-list-right rotate-list-left rotate-list)

(ROTATE-LIST-RIGHT ROTATE-LIST-LEFT ROTATE-LIST)* ;;(rotate-list '(a b c d e))(E A B C D)* ;;(rotate-list '(a b c d e) :direction 'left)(B C D E A)* ;;(rotate-list '(a b c d e) :distance 2)(D E A B C)* ;;(rotate-list '(a b c d e) :direction 'left :distance 2)

Page 29: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(C D E A B)* ;;(untrace)

T* ;;;;;;=====================================================================;; rotate-list version 2;;=====================================================================;;(defun rotate-list (L &KEY direction (distance 1)) (if (eq direction 'left) (rotate-list-left L distance) (rotate-list-right L distance) ) )

ROTATE-LIST* ;;(trace rotate-list-right rotate-list-left rotate-list)

(ROTATE-LIST-RIGHT ROTATE-LIST-LEFT ROTATE-LIST)* ;;(rotate-list '(a b c d e))(E A B C D)* ;;(rotate-list '(a b c d e) :direction 'left)(B C D E A)* ;;(rotate-list '(a b c d e) :distance 2)(D E A B C)* ;;(rotate-list '(a b c d e) :direction 'left :distance 2)(C D E A B)* ;;(untrace)

T* ;;;;=====================================================================(dribble)

*** listing lisp_4/lisp9.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 5 pp 89-90;;=====================================================================;;;;;;=====================================================================;; example of procedure with &AUX parameter;;=====================================================================;;(defun both-ends-v1 (wl) (let* ( (el (first wl)) (tr (last wl))) (cons el tr) ) )

BOTH-ENDS-V1* ;;(both-ends-v1 '(a b c d e))

Page 30: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(A E)* ;;;;(defun both-ends-v2 (wl &AUX (el (first wl)) (tr (last wl))) (cons el tr) )

BOTH-ENDS-V2* ;;(both-ends-v2 '(a b c d e))

(A E)* ;;;;=====================================================================(dribble)

*** listing lisp_5 ***

*** listing lisp_5/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 93-95;;=====================================================================;;;;;;=====================================================================;; Bibliographic Information System - examples;;=====================================================================;;(setf book-ex1 '( (Artificial Intelligence) ; Title (Patrick Henry Winston) ; Author (Technical AI) ; Class ) )Warning: Declaring BOOK-EX1 special.

((ARTIFICIAL INTELLIGENCE) (PATRICK HENRY WINSTON) (TECHNICAL AI))* ;;(second book-ex1)

(PATRICK HENRY WINSTON)* ;;;;=====================================================================;;(setf book-ex2 '( (title (Artificial Intelligence)) ; Title (author (Patrick Henry Winston)) ; Author (class (Technical AI)) ; Class ) )Warning: Declaring BOOK-EX2 special.

((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI)))* ;;(second (assoc 'author book-ex2))

(PATRICK HENRY WINSTON)* ;;;;=====================================================================;;(setf book-ex3 '( (

Page 31: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(title (Artificial Intelligence)) ; Title (author (Patrick Henry Winston)) ; Author (class (Technical AI)) ; Class ) ( (loaned-to (Karen Prendergast)) ; Author (loaned-on (26 May 88)) ; Class ) ) )Warning: Declaring BOOK-EX3 special.

(((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((LOANED-TO (KAREN PRENDERGAST)) (LOANED-ON (26 MAY 88))))* ;;;;=====================================================================;; Examples of READER procedures for the above versions (1, 2, 3);;=====================================================================;;(defun book-author (book) ; for first version of system - book-ex1 (second book) )

BOOK-AUTHOR* ;;(book-author book-ex1)

(PATRICK HENRY WINSTON)* ;;;;=====================================================================;;(defun book-author (book) ; for second version of system - book-ex2 (second (assoc 'author book)) )

BOOK-AUTHOR* ;;(book-author book-ex2)

(PATRICK HENRY WINSTON)* ;;;;=====================================================================;;(defun book-author (book) ; for third version of system - book-ex3 (second (assoc 'author (first book))) )

BOOK-AUTHOR* ;;(book-author book-ex3)

(PATRICK HENRY WINSTON)* ;;;;=====================================================================;;(dribble)

*** listing lisp_5/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 97-99;;=====================================================================

Page 32: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;;;;;=====================================================================;; Bibliographic Information System - examples;;=====================================================================;;;;=====================================================================;; Example of a CONSTRUCTOR for book;;=====================================================================;;(defun make-book (title author class) (list (list 'title title) (list 'author author) (list 'class class) ) )

MAKE-BOOK* ;;(setf book-ex4 (make-book '(Common Lisp) '(Guy Steele) '(Technical Lisp)) )Warning: Declaring BOOK-EX4 special.

((TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASS (TECHNICAL LISP)))* ;;;;=====================================================================;; Examples of READERs for book;;=====================================================================;;(defun book-title (book) (second (assoc 'title book)) )

BOOK-TITLE* ;;(defun book-author (book) (second (assoc 'author book)) )

BOOK-AUTHOR* ;;(defun book-class (book) (second (assoc 'class book)) )

BOOK-CLASS* ;;(book-title book-ex4)

(COMMON LISP)* (book-author book-ex4)

(GUY STEELE)* (book-class book-ex4)

(TECHNICAL LISP)* ;;;;=====================================================================;; Example of a WRITER for book;;=====================================================================;;(defun book-author-writer (book author) (cons (list 'author author) book) )

BOOK-AUTHOR-WRITER

Page 33: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;(setf book-ex4 (book-author-writer book-ex4 '(Guy L Steele)))

((AUTHOR (GUY L STEELE)) (TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASS (TECHNICAL LISP)))* ;;;;=====================================================================;; BETTER Example of a WRITER for book;;=====================================================================;;(defun book-author-writer (book author) (if (eql 'author (first (first book))) (cons (list 'author author) (rest book)) (cons (first book) (book-author-writer (rest book) author)) ) )

BOOK-AUTHOR-WRITER* ;;(setf book-ex4 (make-book '(Common Lisp) '(Guy Steele) '(Technical Lisp)) )

((TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASS (TECHNICAL LISP)))* ;;(setf book-ex4 (book-author-writer book-ex4 '(Guy L Steele)))

((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP)))* ;;;;=====================================================================;;(dribble)

*** listing lisp_5/lisp3.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 99-102;;=====================================================================;;;;;;=====================================================================;; Bibliographic Information System - TRANSFORMERS & FILTERS;;=====================================================================;;;;=====================================================================;; Example of a CONSTRUCTOR for book;;=====================================================================;;(defun make-book (title author class) (list (list 'title title) (list 'author author) (list 'class class) ) )

MAKE-BOOK* ;;;;=====================================================================;; Examples of READERs for book;;=====================================================================;;(defun book-title (book) (second (assoc 'title book))

Page 34: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

)

BOOK-TITLE* ;;(defun book-author (book) (second (assoc 'author book)) )

BOOK-AUTHOR* ;;(defun book-class (book) (second (assoc 'class book)) )

BOOK-CLASS* ;;;;=====================================================================;; Create a bibliographic list;;=====================================================================;;(setf books (list (make-book '(Artificial Intelligence) '(Patrick Henry Winston) '(Technical AI)) (make-book '(Common Lisp) '(Guy L Steele) '(Technical Lisp)) (make-book '(Moby Dick) '(Herman Melville) '(Fiction)) (make-book '(Tom Sawyer) '(Mark Twain) '(Fiction)) (make-book '(The Black Orchid) '(Rex Stout) '(Fiction Mystery)) ) )Warning: Declaring BOOKS special.

(((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))) ((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY))))* ;;;;=====================================================================;; FILTER - generate a list of authors;;=====================================================================;;(defun list-authors (books) (if (endp books) nil (cons (book-author (first books)) (list-authors (rest books)) ) ) )

LIST-AUTHORS* ;;(trace book-author list-authors)

(BOOK-AUTHOR LIST-AUTHORS)* (list-authors books)

Page 35: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT))* (untrace)

T* ;;;;;;=====================================================================;; FILTER - generate a list of fiction books;;=====================================================================;;(defun fictionp (book) (member 'fiction (book-class book)) )

FICTIONP* ;;;;=====================================================================;;(fictionp '( (title (tom sawyer)) (author (mark twain)) (class (fiction))) )

(FICTION)* ;;(fictionp '( (title (common lisp)) (author (guy l steele)) (class (technical lisp))) )

NIL* ;;;;=====================================================================;;(defun list-fiction-books (books) (cond ((endp books) nil) ; empty list? ((fictionp (first books)) ; is fiction? (cons (first books) (list-fiction-books (rest books)) ) ) (t (list-fiction-books (rest books))) ; no - omit ) )

LIST-FICTION-BOOKS* ;;(trace cond endp cons list-fiction-books)(COND ENDP CONS LIST-FICTION-BOOKS)* (list-fiction-books books)(((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY))))* (untrace)

T* ;;;;=====================================================================;; COUNT primitive - count-fiction-books;;=====================================================================;;(defun count-fiction-books (books) (cond ((endp books) 0) ; empty list? ((fictionp (first books)) ; is fiction? (+ 1 (count-fiction-books (rest books)))) (t (count-fiction-books (rest books)))

Page 36: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

) )

COUNT-FICTION-BOOKS* ;;(trace count-fiction-books)

(COUNT-FICTION-BOOKS)* (count-fiction-books books)3* (untrace)

T* ;;;;=====================================================================;; FINDING primitive - find-first-fiction-books;;=====================================================================;;(defun find-first-fiction-books (books) (cond ((endp books) nil) ; empty list? ((fictionp (first books)) ; is fiction? (first books)) (t (find-first-fiction-books (rest books))) ) )

FIND-FIRST-FICTION-BOOKS* ;;(trace find-first-fiction-books)

(FIND-FIRST-FICTION-BOOKS)* (find-first-fiction-books books)((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION)))* (untrace)

T* ;;;;=====================================================================;;(dribble)

*** listing lisp_5/lisp4.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 104-106;;=====================================================================;;;;;;=====================================================================;; Bibliographic Information System - MAPCAR Examples;;=====================================================================;;;;=====================================================================;; Example of a CONSTRUCTOR for book;;=====================================================================;;(defun make-book (title author class) (list (list 'title title) (list 'author author) (list 'class class) ) )

MAKE-BOOK

Page 37: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;;;=====================================================================;; Examples of READERs for book;;=====================================================================;;(defun book-title (book) (second (assoc 'title book)) )

BOOK-TITLE* ;;(defun book-author (book) (second (assoc 'author book)) )

BOOK-AUTHOR* ;;(defun book-class (book) (second (assoc 'class book)) )

BOOK-CLASS* ;;;;=====================================================================;; Example of a PREDICATE for book;;=====================================================================;;(defun fictionp (book) (member 'fiction (book-class book)) )

FICTIONP* ;;;;=====================================================================;; Create a bibliographic list;;=====================================================================;;(setf books (list (make-book '(Artificial Intelligence) '(Patrick Henry Winston) '(Technical AI)) (make-book '(Common Lisp) '(Guy L Steele) '(Technical Lisp)) (make-book '(Moby Dick) '(Herman Melville) '(Fiction)) (make-book '(Tom Sawyer) '(Mark Twain) '(Fiction)) (make-book '(The Black Orchid) '(Rex Stout) '(Fiction Mystery)) ) )Warning: Declaring BOOKS special.

(((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))) ((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY))))* ;;;;=====================================================================;; MAPCAR a primitive over a list

Page 38: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;=====================================================================;;(defun list-authors (books) ; first version (if (endp books) nil (cons (book-author (first books)) (list-authors (rest books)) ) ) )

LIST-AUTHORS* ;;(trace book-author list-authors)

(BOOK-AUTHOR LIST-AUTHORS)* (list-authors books)((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT))* (untrace)

T* ;;(mapcar #'book-author books) ((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT))* ; equivalent MAPCAR version;;;;=====================================================================;; LISP FILTERING examples;;=====================================================================;;(remove-if-not #'fictionp books)

(((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY))))* ;;(remove-if #'fictionp books)

(((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))))* ;;;;=====================================================================;; LISP COUNTING examples;;=====================================================================;;(count-if-not #'fictionp books)

2* ;;(count-if #'fictionp books)

3* ;;;;=====================================================================;; LISP FINDING examples;;=====================================================================;;(find-if-not #'fictionp books)

((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI)))* ;;(find-if #'fictionp books)

Page 39: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION)))* ;;;;=====================================================================;;(dribble)

*

*** listing lisp_5/lisp5.txt ***

* ;;;;=====================================================================;; REF: Higher Order Operations Sheet (from Net);;=====================================================================;;;;;;=====================================================================;; Examples of list searching with FIND;;=====================================================================;;(if (find 3 '(1 2 3 4)) 'yes 'no)

YES* ;;(find 4 '(1 2 3 4))

4* ;;(find 2 '(1 2 3 4))

2* ;;;;=====================================================================;; FIND with :TEST;;=====================================================================;;(find '(a b) '((a b) (c d) (e f)))

NIL* ;;(find '(a b) '((a b) (c d) (e f)) :test #'equal)

(A B)* ;;;;=====================================================================;; FIND with :KEY;;=====================================================================;;(find 'a '((a b) (c d e) (f g h i)) :key #'first)

(A B)* ;;(find 3 '((a b) (c d e) (f g h i)) :key #'length)

(C D E)* ;;;;;;=====================================================================;; FIND with :TEST and :KEY;;=====================================================================;;(find '(d e) '((a b) (c d e) (f g h i)) :key #'rest :test #'equal)

(C D E)* ;;;;=====================================================================

Page 40: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;(dribble)

*** listing lisp_5/lisp6.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 107-108;;=====================================================================;;;;;;=====================================================================;; Examples of LISP primitives FUNCALL, APPLY;;=====================================================================;;;;=====================================================================;; FUNCALL (funcall #'<procedure specification> <arg-1> ...<arg-n>);;=====================================================================;;(funcall #'first '(a b c)) A* ; is equivalent to;;(first '(a b c))

A* ;;(funcall #'append '(a b) '(x y)) (A B X Y)* ; is equivalent to;;(append '(a b) '(x y))

(A B X Y)* ;;;;=====================================================================;; FUNCALL - procedure with procedure argument(s);;=====================================================================;;(defun toss (argument procedure) (funcall procedure argument) )

TOSS* ;;(toss '(victim of lisp programming) #'first)

VICTIM* ;;(toss '(victim of lisp programming) #'rest)

(OF LISP PROGRAMMING)* ;;;;=====================================================================;; APPLY (apply #'<procedure name> <list of arguments>);;=====================================================================;;(apply #'first '((a b c))) A* ; is equivalent to;;(first '(a b c))

A* ;;

Page 41: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(apply #'append '((a b) (x y))) (A B X Y)* ; is equivalent to;;(append '((a b) (x y)))

((A B) (X Y))* ;;;;=====================================================================;;(apply #'+ '(1 2 3 4 5 6))

21* ;;(apply #'+ 1 2 3 '(4 5 6)) 21* ; is treated as the following;;(apply #'+ (append (list 1 2 3) '(4 5 6)))

21* ;;;;=====================================================================;;(dribble)

*** listing lisp_5/lisp7.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 6 pp 109-110;;=====================================================================;;;;;;=====================================================================;; Bibliographic Information System - MAPCAR Examples with LAMBDA exprs;;=====================================================================;;;;=====================================================================;; Example of a CONSTRUCTOR for book;;=====================================================================;;(defun make-book (title author class) (list (list 'title title) (list 'author author) (list 'class class) ) )

MAKE-BOOK* ;;;;=====================================================================;; Examples of READERs for book;;=====================================================================;;(defun book-title (book) (second (assoc 'title book)) )

BOOK-TITLE* ;;(defun book-author (book) (second (assoc 'author book)) )

Page 42: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

BOOK-AUTHOR* ;;(defun book-class (book) (second (assoc 'class book)) )

BOOK-CLASS* ;;(defun book-last-name (book) (first (last (book-author book))) )

BOOK-LAST-NAME* ;;;;=====================================================================;; Create a bibliographic list;;=====================================================================;;(setf books (list (make-book '(Artificial Intelligence) '(Patrick Henry Winston) '(Technical AI)) (make-book '(Common Lisp) '(Guy L Steele) '(Technical Lisp)) (make-book '(Moby Dick) '(Herman Melville) '(Fiction)) (make-book '(Tom Sawyer) '(Mark Twain) '(Fiction)) (make-book '(The Black Orchid) '(Rex Stout) '(Fiction Mystery)) ) )Warning: Declaring BOOKS special.

(((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))) ((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY))))* ;;;;=====================================================================;; MAPCAR a primitive over a list;;=====================================================================;;(mapcar #'book-last-name books)

(WINSTON STEELE MELVILLE TWAIN STOUT)* ;;;;=====================================================================;; Equivalent using a LAMBDA expression instead of a procedure;;=====================================================================;;(mapcar #'(lambda (book) (first (last (book-author book)))) books)

(WINSTON STEELE MELVILLE TWAIN STOUT)* ;;;;=====================================================================;;(dribble)

*** listing lisp_6 ***

Page 43: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

*** listing lisp_6/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 7 pp 113-119;;=====================================================================;;;;;;=====================================================================;; ITERATION examples in LISP;;=====================================================================;;;;=====================================================================;; DOTIMES;;=====================================================================;;(defun dotimes-expt (m n) (let (( result 1)) (dotimes (count n result) (setf result (* m result)) ) ) )

DOTIMES-EXPT* ;;(dotimes-expt 2 3)

8* ;;;;=====================================================================;; DOLIST ;;=====================================================================;;(setf freezing 32 boiling 212)Warning: Declaring FREEZING special.Warning: Declaring BOILING special.

212* ;;(defun count-outlyers (elem-list) (let ((result 0)) (dolist (elem elem-list result) (when (or (> elem boiling) (< elem freezing)) (setf result (+ result 1)) ) ) ) )

COUNT-OUTLYERS* ;;(count-outlyers '( 18 75 31 180 270 52))

3* ;;;;=====================================================================;; DOLIST + RETURN;;=====================================================================;;(defun first-n-outlyers (n elem-list) (let ((result 0) (outlyers nil)) (dolist (elem elem-list outlyers) (cond ((or (> elem boiling) (< elem freezing))

Page 44: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(setf result (+ result 1)) (push elem outlyers)) ((= n result) (return outlyers)) ) ) ) )

FIRST-N-OUTLYERS* ;;(first-n-outlyers 2 '( 18 75 31 180 270 52))

(31 18)* ;;;;=====================================================================;; DO - version 1;;=====================================================================;;(defun do-expt (m n) (do ((result 1) ; bind parameters (exponent n)) ; bind parameters

((zerop exponent) result) ; TEST / RETURN

(setf result (* m result)) ; body (setf exponent (- exponent 1)) ; body ) )

DO-EXPT* ;;(do-expt 4 4)

256* ;;;;=====================================================================;; DO - version 2 (REGARDED AS OLD FASHIONED BY THIS SYSTEM !!!);;=====================================================================;;;; (defun do-expt (m n);; (do ((result 1) ; bind parameters;; (exponent n)) ; bind parameters;; () ; test always fails;; (when (zerop exponent);; (return result)) ; RETURN;; ;; (setf result (* m result)) ; body;; (setf exponent (- exponent 1)) ; body;; );; );;;; (do-expt 4 4);;;;=====================================================================;; DO - version 3;;=====================================================================;;(defun do-expt (m n) (do ((result 1 (* m result)) ; init / update (exponent n (- exponent 1))) ; init / update ((zerop exponent) result) ; RETURN ) ; no body !!! )

DO-EXPT* ;;(do-expt 4 4)

Page 45: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

256* ;;;;=====================================================================;;(dribble)

*** listing lisp_6/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 7 pp 120-121;;=====================================================================;;;;;;=====================================================================;; ITERATION examples in LISP;;=====================================================================;;;;=====================================================================;; LOOP;;=====================================================================;;(setf cheers '(cheer cheer cheer))Warning: Declaring CHEERS special.

(CHEER CHEER CHEER)* (setf loop-count 0)Warning: Declaring LOOP-COUNT special.

0* ;;(loop (when (endp cheers) (return loop-count)) ; RETURN (setf cheers (rest cheers)) (setf loop-count (+ loop-count 1)) )

3* ;;;;=====================================================================;; PROG1 & PROGN;;=====================================================================;;(prog1 (setf a 'x) (setf b 'y) (setf c 'z))

Warning: These variables are undefined: A B C

X* ;;(progn (setf a 'x) (setf b 'y) (setf c 'z))Warning: Declaring A special.Warning: Declaring B special.Warning: Declaring C special.

Z* ;;;;=====================================================================;;(dribble)

*** listing lisp_7 ***

*** listing lisp_7/lisp1.txt ***

Page 46: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 9 pp 131-132;;=====================================================================;;;;;;=====================================================================;; PRINTING & READING examples in LISP;;=====================================================================;;;;=====================================================================;; PRINT Examples;;=====================================================================;;(setf temp 100)Warning: Declaring TEMP special.

100* (print temp)

100 100* (if (< -1 (print (- temp 98.6)) +1) 'normal 'abnormal)

1.4000015 ABNORMAL* ;;;;=====================================================================;; PRINT & READ Example;;=====================================================================;;(let ((p nil)) (print '(please type a patient name)) (setf p (read)) (print (append '(OK the name is) (list p))) p )

(PLEASE TYPE A PATIENT NAME) ross

(OK THE NAME IS ROSS) ROSS* ;;;;=====================================================================;;(dribble)

*** listing lisp_7/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 9 pp 133-134;;=====================================================================;;;;;;=====================================================================;; FORMATTING examples in LISP;;=====================================================================;;(format t "Hello!")Hello!

Page 47: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

NIL* (format t "~%Hello!")

Hello!NIL* (format t "~%Hello!~%I'm ready to start now.")

Hello!I'm ready to start now.NIL* ;;(progn (format t "~%Line followed by a % directive. ~%") (format t "~%Line preceded by a % directive. ~%") (format t "~&Line preceded by a & directive. ") )

Line followed by a % directive.

Line preceded by a % directive. Line preceded by a & directive. NIL* ;;(setf name 'kirsh)Warning: Declaring NAME special.

KIRSH* (setf symptoms '(fever rash nausea))Warning: Declaring SYMPTOMS special.

(FEVER RASH NAUSEA)* ;;(format t "~%The next patient is ~a." name)

The next patient is KIRSH.NIL* ;;(format t "~%The patient ~a presented ~a symptoms ~a." name (length symptoms) symptoms)

The patient KIRSH presented 3 symptoms (FEVER RASH NAUSEA).NIL* ;;(format t "~%Patient: ~10aSymptoms ~a." name (length symptoms))

Patient: KIRSH Symptoms 3.NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_7/lisp3.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 9 pp 135-139;;=====================================================================;;;;;;=====================================================================;; FILE HANDLING examples in LISP;;=====================================================================;;(with-open-file (pat-stream "lisp3.dat" :direction :input)

Page 48: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(dotimes (n 2) (print (read pat-stream))))

((DAVID KIRSH) (FEVER RASH)) ((GEORGE HEGEL) (FEVER HEADACHE)) NIL* ;;(with-open-file (pat-stream "lisp3.dat" :direction :input)(do ((patient (read pat-stream nil) (read pat-stream nil))) ((not patient)) (print patient)))

((DAVID KIRSH) (FEVER RASH)) ((GEORGE HEGEL) (FEVER HEADACHE)) ((IMMANUEL KANT) (NAUSEA)) ((RENE DESCARTES) (NAUSEA)) ((JEAN-PAUL SATRE) (NAUSEA STOMACHACHE)) NIL* ;;(defun nausea-p (descr) (member 'nausea (second descr)) )

NAUSEA-P* (with-open-file (pat-stream "lisp3.dat" :direction :input) (with-open-file (nas-stream "lisp3.nas" :direction :output) (do ((pdescr (read pat-stream nil) (read pat-stream nil))) ((not pdescr)) (when (nausea-p pdescr) (print pdescr nas-stream)))))

NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_7/lisp4.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 9 pp 140-143;;=====================================================================;;;;;;=====================================================================;; READ & EVAL examples in LISP;;=====================================================================;;(setf form-to-eval '(+ 2 2))Warning: Declaring FORM-TO-EVAL special.

(+ 2 2)* form-to-eval

(+ 2 2)* (eval form-to-eval)

4* (read)(+ 2 2)

(+ 2 2)* (eval (read))

Page 49: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(+ 2 2)

4* ;;;;=====================================================================;; STRING and CHARACTER examples in LISP;;=====================================================================;;(length '(a b c))

3* (length "abc")

3* ;;(reverse '(a b c))

(C B A)* (reverse "abc")

"cba"* ;;(elt '(a b c) 0)

A* (elt '(a b c) 2)

C* ;;(elt "abc" 0)

#\a* (elt "abc" 2)

#\c* ;;(string= "abc" "xyz")

NIL* (string= "abc" "abc")

T* (string= "abc" "ABC")

NIL* ;;(string-equal "abc" "xyz")

NIL* (string-equal "abc" "abc")

T* (string-equal "abc" "ABC")

T* ;;(char= #\a #\b)

NIL* (char= #\a #\a)

T* (char= #\a #\A)

NIL* ;;(char-equal #\a #\b)

Page 50: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

NIL* (char-equal #\a #\a)

T* (char-equal #\a #\A)

T* ;;(search "Katz" "Katz, Boris")

0* (search "Boris" "Katz, Boris")

6* (search "Pushkin" "Katz, Boris")

NIL* ;;(search "BORIS" "Katz, Boris")

NIL* (search "BORIS" "Katz, Boris" :test #'char-equal)

6* ;;(search '(katz) '(katz boris))

0* (search '(boris) '(katz boris))

1* (search '(pushkin) '(katz boris))

NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_7/lisp5.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 9 pp 143-145;;=====================================================================;;;;;;=====================================================================;; READ-LINE & READ_CHAR examples in LISP;;=====================================================================;;(read-line)"This is a read-line test."

"\"This is a read-line test.\""NIL* ;;(read-char)x#\x* (read-char)X#\X

Page 51: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;(defun fetch (fragment file) (with-open-file (line-stream file :direction :input) (do ((line (read-line line-stream nil) (read-line line-stream nil))) ((not line) (format t "~%No such entry!")) (when (search fragment line :test #'char-equal) (format t "~%~a" line) (return t)))) )

FETCH* ;;(fetch "Katz" "people.ail")

Katz, Boris 6032 BORIST* (fetch "Pushkin" "people.ail")

No such entry!NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_8 ***

*** listing lisp_8/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 11 pp 160-166;;=====================================================================;;;;;;=====================================================================;; STRUCTURED DATA examples in LISP;;=====================================================================;;(setf (get ' patrick 'parents) '(robert dorothy))

(ROBERT DOROTHY)* (get 'patrick 'parents)

(ROBERT DOROTHY)* ;;(setf (get 'bag 'contents) '(bread butter))

(BREAD BUTTER)* (get 'bag 'contents)

(BREAD BUTTER)* (remprop 'bag 'contents)

(CONTENTS (BREAD BUTTER))* (get 'bag 'contents)

NIL* ;;;;;;=====================================================================;; ARRAY examples in LISP;;=====================================================================

Page 52: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

;;(setf part-bins (make-array '(4)))Warning: Declaring PART-BINS special.

#(0 0 0 0)* (setf part-bins (make-array 4 :initial-element 'e))

#(E E E E)* ;;(setf check-board (make-array '(8 8)))Warning: Declaring CHECK-BOARD special.

#2A((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))* (setf check-board (make-array '(8 8) :initial-contents '((X B X B X B X B) (B X B X B X B X) (X B X B X B X B) (E X E X E X E X) (X E X E X E X E) (W X W X W X W X) (X W X W X W X W) (W X W X W X W X) )))

#2A((X B X B X B X B) (B X B X B X B X) (X B X B X B X B) (E X E X E X E X) (X E X E X E X E) (W X W X W X W X) (X W X W X W X W) (W X W X W X W X))* ;;(setf (aref part-bins 0) 'nails)

NAILS* (setf (aref part-bins 1) 'nuts)

NUTS* (setf (aref part-bins 2) 'bolts)

BOLTS* (setf (aref part-bins 3) 'washers)

WASHERS* part-bins

#(NAILS NUTS BOLTS WASHERS)* ;;(defun cbwsp (part) (let ((result 0)) (dotimes (n 4 result) (when (eq part (aref part-bins n)) (setf result (+ result 1)) ) ) ) )

Page 53: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

CBWSP* (cbwsp 'washers)

1* ;;(defun cbwsp2 (part array) (let ((result 0)) (dotimes (n (array-dimension array 0) result) (when (eq part (aref array n)) (setf result (+ result 1)) ) ) ) )

CBWSP2* (cbwsp2 'washers part-bins)

1* ;;;;=====================================================================;;(dribble)

*** listing lisp_8/lisp2.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 12 pp 160-166;;=====================================================================;;;;;;=====================================================================;; MACRO examples in LISP;;=====================================================================;;(defun when-plusp (n result) (when (plusp n) result)) WHEN-PLUSP* ;;BUGGED !!!;;;;(setf pressure -3)Warning: Declaring PRESSURE special.

-3* (when-plusp pressure (print 'alarm))

ALARM NIL* (setf pressure 3)

3* (when-plusp pressure (print 'alarm))

ALARM ALARM* ;;;;=====================================================================;;(defmacro w-p-macro (n result) (list 'when (list 'plusp n) result))

W-P-MACRO* ;;(setf pressure 3)

Page 54: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

3* (w-p-macro pressure (print 'alarm))

ALARM ALARM* (setf pressure -3)

-3* (w-p-macro pressure (print 'alarm))

NIL* ;;;;=====================================================================;; BACKQUOTE examples in LISP;;=====================================================================;;(setf variable 'test)Warning: Declaring VARIABLE special.

TEST* `(this is a ,variable)

(THIS IS A TEST)* ;;(setf variable '(more difficult example))

(MORE DIFFICULT EXAMPLE)* `(this is a ,variable)

(THIS IS A (MORE DIFFICULT EXAMPLE))* `(this is a ,@variable)

(THIS IS A MORE DIFFICULT EXAMPLE)* ;;;;=====================================================================;; BACKQUOTE & MACRO examples in LISP;;=====================================================================;;(defmacro w-p-macro2 (n result) ;; sample translation ;; (w-p-macro2 p (print 'alarm)) ;; => (when (plus p) (print 'alarm)) `(when (plusp ,n) ,result))

W-P-MACRO2* ;;(setf pressure 3)

3* (w-p-macro2 pressure (print 'alarm))

ALARM ALARM* (setf pressure -3)

-3* (w-p-macro2 pressure (print 'alarm))

NIL* ;;;;=====================================================================;;(dribble)

*** listing lisp_8/lisp3.txt ***

Page 55: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 13 pp 175-181;;=====================================================================;;;;;;=====================================================================;; STRUCTURE examples in LISP;;=====================================================================;;(defstruct person (sex nil) ;default value is nil (personality 'nice) ;default value is nice )

PERSON* ;;(setf pi1 (make-person))Warning: Declaring PI1 special.

#S(PERSON :SEX NIL :PERSONALITY NICE)* (person-sex pi1)

NIL* (person-personality pi1)

NICE* (person-p pi1)

T* ;;(setf pi2 (make-person :sex 'female))Warning: Declaring PI2 special.

#S(PERSON :SEX FEMALE :PERSONALITY NICE)* (person-sex pi2)

FEMALE* (person-personality pi2)

NICE* (person-p pi2)

T* (person-p '(a b c))

NIL* ;;(describe pi1)

#S(PERSON :SEX NIL :PERSONALITY NICE) is a structure of type PERSON.SEX: NIL.PERSONALITY: NICE.* (describe pi2)

#S(PERSON :SEX FEMALE :PERSONALITY NICE) is a structure of type PERSON.SEX: FEMALE.PERSONALITY: NICE.* ;;;;=====================================================================;; NESTED STRUCTURE examples in LISP;;=====================================================================;;(defstruct employee (lenserv 0)

Page 56: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

(payment 'salary) )

EMPLOYEE* (defstruct (hacker (:include employee)) (pref-lang 'lisp) )

HACKER* ;;(setf emp (make-employee))Warning: Declaring EMP special.

#S(EMPLOYEE :LENSERV 0 :PAYMENT SALARY)* (setf hack (make-hacker))Warning: Declaring HACK special.

#S(HACKER :LENSERV 0 :PAYMENT SALARY :PREF-LANG LISP)* (describe emp)

#S(EMPLOYEE :LENSERV 0 :PAYMENT SALARY) is a structure of type EMPLOYEE.LENSERV: 0.PAYMENT: SALARY.* (describe hack)

#S(HACKER :LENSERV 0 :PAYMENT SALARY :PREF-LANG LISP) is a structure of type HACKER.LENSERV: 0.PAYMENT: SALARY.PREF-LANG: LISP.* ;;(employee-lenserv emp)

0* (hacker-lenserv hack)

0* (employee-lenserv hack)

0* (employee-payment hack)

SALARY* (hacker-pref-lang hack)

LISP* ;;(defstruct (salesp (:include employee (payment 'commission))) (pref-car 'mercedes) )

SALESP* ;;(setf salep (make-salesp))Warning: Declaring SALEP special.

#S(SALESP :LENSERV 0 :PAYMENT COMMISSION :PREF-CAR MERCEDES)* (describe salep)

#S(SALESP :LENSERV 0 :PAYMENT COMMISSION :PREF-CAR MERCEDES) is a structure of type SALESP.LENSERV: 0.PAYMENT: COMMISSION.PREF-CAR: MERCEDES.* (employee-payment hack)

SALARY

Page 57: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

* (employee-payment salep)

COMMISSION* (salesp-pref-car salep)

MERCEDES* ;;;;=====================================================================(dribble)

*** listing lisp_9 ***

*** listing lisp_9/lisp1.txt ***

* ;;;;=====================================================================;; REF: LISP (3rd Ed.) Winston & Horn [Addison Wesley 1993];; Chapter 15 pp 218-220;;=====================================================================;;;;;;=====================================================================;; lambda functions, LEXICAL VARIABLES in LISP (encapsulation);;=====================================================================;;(setf sq-proc #'(lambda (n) (* n n)))Warning: Declaring SQ-PROC special.

#<Interpreted Function (LAMBDA (N) (* N N)) {7009569}>* (mapcar sq-proc '(1 2 3))

(1 4 9)* (funcall sq-proc 2)

4* ;;;;=====================================================================;;(setf po2 #'(lambda () (setf ppo2 (* ppo2 2))))Warning: Declaring PO2 special.

#<Interpreted Function (LAMBDA () (SETF PPO2 (* PPO2 2))) {700D9F9}>* (setf ppo2 1)Warning: Declaring PPO2 special.

1* (funcall po2)

2* (funcall po2)

4* (funcall po2)

8* ;;;;=====================================================================;;(setf po2-2 (let ((ppo2-2 1)) #'(lambda () (setf ppo2-2 (* ppo2-2 2))) ) )Warning: Declaring PO2-2 special.

Page 58: Start of list all Lisp examples - Karlstad University · 2013-05-18 · *** Start of list all Lisp examples *** *** listing lisp_1 *** *** listing lisp_1/lisp1.txt *** * ;;;;=====;;

#<Interpreted Function "LET ((PPO2-2 1))" {7015619}>* ;;(funcall po2-2)

2* (funcall po2-2)

4* (funcall po2-2)

8* ;;;;=====================================================================;;(dribble)

*** End of list all Lisp examples ***