Transcript
Page 1: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

CMPUT 325 - Lisp Basics

Dr. B. Price & Dr. R. Greiner

16th September 2004

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 1

Page 2: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp History

I History: �LISt Processing� Speci�ed by McCarthy in 1958, butstill in use.

I Inspired by Alonzo Church's abstract theory of computations:�lambda calculus� in 1930's

I Second high-level language after Fortran. First Language tosupport:

I structured IF_THEN_ELSE_ENDIFI dynamic typing of variables, recursion

I Dialects of Lisp: Pure Lisp, Franz Lisp, MacLisp, InterLisp,Common Lisp (now largely standardized on Common Lisp)

I Supports functional, procedural, object-oriented and genericprogramming

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 2

Page 3: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp History

I History: �LISt Processing� Speci�ed by McCarthy in 1958, butstill in use.

I Inspired by Alonzo Church's abstract theory of computations:�lambda calculus� in 1930's

I Second high-level language after Fortran. First Language tosupport:

I structured IF_THEN_ELSE_ENDIFI dynamic typing of variables, recursion

I Dialects of Lisp: Pure Lisp, Franz Lisp, MacLisp, InterLisp,Common Lisp (now largely standardized on Common Lisp)

I Supports functional, procedural, object-oriented and genericprogramming

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 2

Page 4: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp History

I History: �LISt Processing� Speci�ed by McCarthy in 1958, butstill in use.

I Inspired by Alonzo Church's abstract theory of computations:�lambda calculus� in 1930's

I Second high-level language after Fortran. First Language tosupport:

I structured IF_THEN_ELSE_ENDIFI dynamic typing of variables, recursion

I Dialects of Lisp: Pure Lisp, Franz Lisp, MacLisp, InterLisp,Common Lisp (now largely standardized on Common Lisp)

I Supports functional, procedural, object-oriented and genericprogramming

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 2

Page 5: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp History

I History: �LISt Processing� Speci�ed by McCarthy in 1958, butstill in use.

I Inspired by Alonzo Church's abstract theory of computations:�lambda calculus� in 1930's

I Second high-level language after Fortran. First Language tosupport:

I structured IF_THEN_ELSE_ENDIFI dynamic typing of variables, recursion

I Dialects of Lisp: Pure Lisp, Franz Lisp, MacLisp, InterLisp,Common Lisp (now largely standardized on Common Lisp)

I Supports functional, procedural, object-oriented and genericprogramming

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 2

Page 6: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp History

I History: �LISt Processing� Speci�ed by McCarthy in 1958, butstill in use.

I Inspired by Alonzo Church's abstract theory of computations:�lambda calculus� in 1930's

I Second high-level language after Fortran. First Language tosupport:

I structured IF_THEN_ELSE_ENDIFI dynamic typing of variables, recursion

I Dialects of Lisp: Pure Lisp, Franz Lisp, MacLisp, InterLisp,Common Lisp (now largely standardized on Common Lisp)

I Supports functional, procedural, object-oriented and genericprogramming

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 2

Page 7: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...

> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 8: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)

34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 9: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34

> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 10: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}

12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 11: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12

> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 12: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)

29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 13: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29

> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 14: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))

sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 15: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq

> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 16: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)

144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 17: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144

> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 18: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp is Interactive

ohaton: > gclGCL (GNU Common Lisp) Version(2.2)Licensed under GNU Public Library License ...> (+ 11 23)34> (SETF x (* 3 4)) {note: procedural!}12> (+ (* x 2) 5)29> (DEFUN sq (y) (* y y))sq> (sq x)144> (EXIT)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 3

Page 19: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lecture Note Notation

I Sitting at the Lisp interpreter, things look like this:

> (+ 3 4)7

I For compactness in the lecture notes, we write:

(+ 3 4) → 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 4

Page 20: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 21: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 22: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y →

"Error Undefined!"'Y → Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 23: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"

'Y → Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 24: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y →

Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 25: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y

(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 26: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) →

3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 27: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) → 3

'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 28: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) → 3'(+ 1 2) →

(+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 29: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Controlling Evaluation in LISP

I By default, LISP attempts to evaluate the expressions youenter

I To enter a constant that should not be evaluated, preface itwith a quote

y → "Error Undefined!"'Y → Y(+ 1 2) → 3'(+ 1 2) → (+ 1 2)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 5

Page 30: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp's Data Structures

I In "pure" Lisp all compound data is represented by�symbolic expressions� (called s-expressions, or s-exprs)

I An s-expr is eitherI an Atom (e.g., 1 able nil t 3.4)I or a List of s-exprs (e.g., (1 2 3))

I An Atom is a number, ora string of 1 or more letters or digits.

e.g.

g u-of-a

24 cdr

1a2b 8088

I Modern Lisp's also implement vectors, hash tables, arrays,various types of numbers and even objects

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 6

Page 31: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp's Data Structures

I In "pure" Lisp all compound data is represented by�symbolic expressions� (called s-expressions, or s-exprs)

I An s-expr is eitherI an Atom (e.g., 1 able nil t 3.4)I or a List of s-exprs (e.g., (1 2 3))

I An Atom is a number, ora string of 1 or more letters or digits.

e.g.

g u-of-a

24 cdr

1a2b 8088

I Modern Lisp's also implement vectors, hash tables, arrays,various types of numbers and even objects

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 6

Page 32: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp's Data Structures

I In "pure" Lisp all compound data is represented by�symbolic expressions� (called s-expressions, or s-exprs)

I An s-expr is eitherI an Atom (e.g., 1 able nil t 3.4)I or a List of s-exprs (e.g., (1 2 3))

I An Atom is a number, ora string of 1 or more letters or digits.

e.g.

g u-of-a

24 cdr

1a2b 8088

I Modern Lisp's also implement vectors, hash tables, arrays,various types of numbers and even objects

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 6

Page 33: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lisp's Data Structures

I In "pure" Lisp all compound data is represented by�symbolic expressions� (called s-expressions, or s-exprs)

I An s-expr is eitherI an Atom (e.g., 1 able nil t 3.4)I or a List of s-exprs (e.g., (1 2 3))

I An Atom is a number, ora string of 1 or more letters or digits.

e.g.

g u-of-a

24 cdr

1a2b 8088

I Modern Lisp's also implement vectors, hash tables, arrays,various types of numbers and even objects

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 6

Page 34: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lists in LISP

I Def'n: A list is 0 or more s-exprs enclosed in parentheses.

I Examples

(a b c)

()

(+ 2 3)

(plus x (times y 3))

( () () )

I Generally: (s1 s2 · · · sn), n ≥ 0, si are s-exprs

I Special case: () is the empty list. Also called nil.[It is both an atom and a list.]

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 7

Page 35: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lists in LISP

I Def'n: A list is 0 or more s-exprs enclosed in parentheses.

I Examples

(a b c)

()

(+ 2 3)

(plus x (times y 3))

( () () )

I Generally: (s1 s2 · · · sn), n ≥ 0, si are s-exprs

I Special case: () is the empty list. Also called nil.[It is both an atom and a list.]

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 7

Page 36: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lists in LISP

I Def'n: A list is 0 or more s-exprs enclosed in parentheses.

I Examples

(a b c)

()

(+ 2 3)

(plus x (times y 3))

( () () )

I Generally: (s1 s2 · · · sn), n ≥ 0, si are s-exprs

I Special case: () is the empty list. Also called nil.[It is both an atom and a list.]

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 7

Page 37: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Lists in LISP

I Def'n: A list is 0 or more s-exprs enclosed in parentheses.

I Examples

(a b c)

()

(+ 2 3)

(plus x (times y 3))

( () () )

I Generally: (s1 s2 · · · sn), n ≥ 0, si are s-exprs

I Special case: () is the empty list. Also called nil.[It is both an atom and a list.]

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 7

Page 38: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Properties of Lists

I The size of a list does not have to be declared in advance.

I Di�ers from set (why?):

I can contain multiple instances of the same elementI elements are in a strict order.

I Eg Lists

(b a c b)

(1 2 3)

(1 2 (3 4) 5 () )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 8

Page 39: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Properties of Lists

I The size of a list does not have to be declared in advance.

I Di�ers from set (why?):

I can contain multiple instances of the same elementI elements are in a strict order.

I Eg Lists

(b a c b)

(1 2 3)

(1 2 (3 4) 5 () )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 8

Page 40: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Properties of Lists

I The size of a list does not have to be declared in advance.

I Di�ers from set (why?):

I can contain multiple instances of the same element

I elements are in a strict order.

I Eg Lists

(b a c b)

(1 2 3)

(1 2 (3 4) 5 () )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 8

Page 41: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Properties of Lists

I The size of a list does not have to be declared in advance.

I Di�ers from set (why?):

I can contain multiple instances of the same elementI elements are in a strict order.

I Eg Lists

(b a c b)

(1 2 3)

(1 2 (3 4) 5 () )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 8

Page 42: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Properties of Lists

I The size of a list does not have to be declared in advance.

I Di�ers from set (why?):

I can contain multiple instances of the same elementI elements are in a strict order.

I Eg Lists

(b a c b)

(1 2 3)

(1 2 (3 4) 5 () )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 8

Page 43: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →

(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 44: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 45: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 46: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →

'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 47: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 48: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) →

(1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 49: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)

(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 50: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) →

(1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 51: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)

In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 52: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→

(s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 53: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Building up Compound s-expr

I CONS builds an s-expr from 2 others

(CONS 1 2) →(1 . 2)

I The 2 item result is also known as a "CONS" cell

I If the second argument is a list, Lisp displays the result as a list

(CONS 1 '(2 3)) →'(1 . (2 3)) ≡ ( 1 2 3 )

I When the second argument is "nil" (a.k.a empty list) we get asingleton

(CONS 1 nil) → (1 . nil) ≡ (1)(CONS 1 ()) → (1)In general(CONS s0 (s1 . . . sn))→ (s0 . (s1 . . . sn)) ≡ (s0 s1 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 9

Page 54: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) →

s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 55: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 56: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) →

s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 57: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 58: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 59: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) →

s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 60: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 61: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) →

(s2 s3 . . . sn)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 62: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Taking an s-expr apart

I Given s0 ≡ ( s1 . s2 ), where s1 and s2 are expressions, CARreturns �rst element

(CAR '(s1 . s2)) → s1

I The CDR function returns the second element

(CDR '(s1 . s2)) → s2

I Recall, a list is a CONS cell whose second element is a list

(s1 s2 s3 . . . sn) ≡ ( s1 . (s2 s3 . . . sn) )

I The CAR function returns the "�rst" element of the list

(CAR '(s1 s2 s3 . . . sn)) → s1

I The CDR function returns the "rest" of the list

(CDR '(s1 s2 s3 . . . sn)) → (s2 s3 . . . sn)Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 10

Page 63: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Why 'car' and 'cdr'

I Historical:

I The IBM 704 divided words into "address" and "decrement"I CAR was assembly instruction to extract the

"Contents of the Address Register", andI CDR extracted the "Contents of the Decrement Register".I The name of Lisp's core functions derive from low-level

assembly instructions!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 11

Page 64: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Why 'car' and 'cdr'

I Historical:

I The IBM 704 divided words into "address" and "decrement"

I CAR was assembly instruction to extract the"Contents of the Address Register", and

I CDR extracted the "Contents of the Decrement Register".I The name of Lisp's core functions derive from low-level

assembly instructions!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 11

Page 65: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Why 'car' and 'cdr'

I Historical:

I The IBM 704 divided words into "address" and "decrement"I CAR was assembly instruction to extract the

"Contents of the Address Register", and

I CDR extracted the "Contents of the Decrement Register".I The name of Lisp's core functions derive from low-level

assembly instructions!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 11

Page 66: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Why 'car' and 'cdr'

I Historical:

I The IBM 704 divided words into "address" and "decrement"I CAR was assembly instruction to extract the

"Contents of the Address Register", andI CDR extracted the "Contents of the Decrement Register".

I The name of Lisp's core functions derive from low-levelassembly instructions!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 11

Page 67: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Why 'car' and 'cdr'

I Historical:

I The IBM 704 divided words into "address" and "decrement"I CAR was assembly instruction to extract the

"Contents of the Address Register", andI CDR extracted the "Contents of the Decrement Register".I The name of Lisp's core functions derive from low-level

assembly instructions!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 11

Page 68: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 69: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) →

A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 70: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A

(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 71: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) →

(B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 72: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 73: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) →

(B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 74: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 75: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 76: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) →

C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 77: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 78: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

FIRST, REST and the Rest

I CAR, CDR are traditional, but modern FIRST and REST aremore readable

(FIRST '(A B C)) → A(REST '(A B C)) → (B C)

I The CAR and CDR functions can be composed

(CAR (CDR '(A (B) C)) → (B)

I LISP provides abbreviations for common sequences of accessors

(CAR (CDR '(A B C)) ≡(CADR '(A B C))

I Any combination of CAR's and CDR's are de�ned up to 10

(CAADDR '(A B (C) D )) → C

I Additional accessors: FIRST, SECOND, ..., TENTH

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 12

Page 79: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) →

( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 80: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )

(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 81: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) →

(1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 82: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)

(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 83: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) →

(1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 84: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)

(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 85: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →

( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 86: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )

(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 87: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) →

( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 88: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )

(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 89: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→

(nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 90: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)

(CONS (CONS '(1 2) '(3 4)) '(5 6)) →( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 91: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 92: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Examples of CONS usage

(CONS 1 2) → ( 1 . 2 )(CONS 1 nil) → (1)(CONS 1 '(2 3)) → (1 2 3)(CONS '(1 2) nil) ) →( (1 2) )(CONS '(1 2) '(2 3) ) → ( (1 2) 2 3 )(CONS nil 1)→ (nil . 1)(CONS (CONS '(1 2) '(3 4)) '(5 6)) →

( ( (1 2) 3 4) 5 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 13

Page 93: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates

I Predicate ≡ A Boolean-Valued Function

I Returns: True or False

I The Lisp representation:NIL ≡ FalseAny non-NIL s-expression ≡ True

I Conventionally, t is used to represent true(Note t is a non-NIL atom).

I When the atom t is returned by LISP, it is displayed "T".

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 14

Page 94: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates

I Predicate ≡ A Boolean-Valued Function

I Returns: True or False

I The Lisp representation:NIL ≡ FalseAny non-NIL s-expression ≡ True

I Conventionally, t is used to represent true(Note t is a non-NIL atom).

I When the atom t is returned by LISP, it is displayed "T".

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 14

Page 95: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates

I Predicate ≡ A Boolean-Valued Function

I Returns: True or False

I The Lisp representation:NIL ≡ FalseAny non-NIL s-expression ≡ True

I Conventionally, t is used to represent true(Note t is a non-NIL atom).

I When the atom t is returned by LISP, it is displayed "T".

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 14

Page 96: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates

I Predicate ≡ A Boolean-Valued Function

I Returns: True or False

I The Lisp representation:NIL ≡ FalseAny non-NIL s-expression ≡ True

I Conventionally, t is used to represent true(Note t is a non-NIL atom).

I When the atom t is returned by LISP, it is displayed "T".

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 14

Page 97: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates

I Predicate ≡ A Boolean-Valued Function

I Returns: True or False

I The Lisp representation:NIL ≡ FalseAny non-NIL s-expression ≡ True

I Conventionally, t is used to represent true(Note t is a non-NIL atom).

I When the atom t is returned by LISP, it is displayed "T".

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 14

Page 98: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→

T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 99: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T

(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 100: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→

NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 101: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL

(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 102: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→

NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 103: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL

(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 104: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→

T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 105: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T

(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 106: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→

NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 107: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL

(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 108: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→

T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 109: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: ATOM

I ATOM tests if s-expr is atomic.

Examples:

(ATOM 'a)→ T(ATOM '(a))→ NIL(SETQ a '(1 2))(ATOM a )→ NIL(ATOM 1)→ T(ATOM '( 1 . 2) )→ NIL(ATOM nil )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 15

Page 110: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→

T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 111: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T

(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 112: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→

NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 113: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL

(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 114: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→

NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 115: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL

(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 116: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→

T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 117: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T

(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 118: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→

T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 119: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T

(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 120: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→

T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 121: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T

(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 122: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→

"Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 123: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: LISTP

I LISTP tests if an s-expr is a cons cell or the empty list.

Examples

(LISTP nil )→ T(LISTP 'a )→ NIL(LISTP 1 )→ NIL(LISTP '(1 2))→ T(LISTP '(1 . 2))→ T(LISTP (CONS 1 2))→ T(LISTP b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 16

Page 124: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→

T(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 125: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T

(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 126: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→

nil(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 127: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→ nil

(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 128: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→

T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 129: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→ T

(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 130: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→

T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 131: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQUAL

I Compares two s-expr for equality:"having the same value"

(EQUAL 1 1)→ T(EQUAL 1 2)→ nil(EQUAL nil (CDR '(a))→ T(EQUAL '(a b) '(a b) )→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 17

Page 132: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→

T(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 133: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T

(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 134: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→

nil(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 135: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→ nil

(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 136: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→

T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 137: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→ T

(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 138: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→

T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 139: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: EQ

I EQ compares two atoms for equivalence:"sharing the same representation"

(EQ 1 1)→ T(EQ '(1 2) '(1 2))→ nil(EQUAL '(1 2) '(1 2))→ T(SETF a '(1 2))(EQ a a)→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 18

Page 140: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→

T(= 1 2)→ NIL(= 'a 'b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 141: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→ T

(= 1 2)→ NIL(= 'a 'b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 142: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→ T(= 1 2)→

NIL(= 'a 'b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 143: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→ T(= 1 2)→ NIL

(= 'a 'b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 144: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→ T(= 1 2)→ NIL(= 'a 'b)→

"Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 145: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: =

I Compares two numbers

(= 1 1)→ T(= 1 2)→ NIL(= 'a 'b)→ "Error!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 19

Page 146: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 147: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→

T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 148: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T

(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 149: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→

T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 150: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T

(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 151: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→

NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 152: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL

(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 153: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→

NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 154: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL

(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 155: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→

Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 156: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 157: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 158: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→

T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 159: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicates: NULL and NOT

I NULL tests if s-expr is empty list (or the NIL atom)

(NULL ())→ T(NULL nil)→ T(NULL 't )→ NIL(NULL '(1))→ NIL(NULL 1 2)→ Error - Null takes 1 argument

I NULL works like a negation operator

(NULL (NULL '(1 2 3)))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 20

Page 160: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→

NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 161: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 162: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→

T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 163: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 164: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 165: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→

T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 166: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Predicate: NOT

I (NOT t)→ NIL

I (NOT nil)→ T

I Convention: save NULL for lists, use NOT for logical negation

(NOT (> 5 4))→ T

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 21

Page 167: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispLisp HistorySymbolic ExpressionsPredicates

Other Useful LISP Predicates

I Type predicates:

I symbolpI conspI numberpI stringpI functionp

I Numerical comparison predicates: > < = <= >=

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 22

Page 168: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LISP Forms

I A form is a syntactic expression that can be evaluated by LISP

I In general: A FORM is one of

I a constant(e.g., t, nil, a number, . . . )

I a variableI a compound form

(fn a1 ... an)where fn is a symbol, andeach ai is a form

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 23

Page 169: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LISP Forms

I A form is a syntactic expression that can be evaluated by LISP

I In general: A FORM is one of

I a constant(e.g., t, nil, a number, . . . )

I a variableI a compound form

(fn a1 ... an)where fn is a symbol, andeach ai is a form

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 23

Page 170: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LISP Forms

I Evaluation of a form results in an s-expression� the internalrepresentation used by LISP

I Up to now we have not distinguished the underlyings-expression from the printed syntax humans use tocommunicate them

I Each type of form de�nes how the syntax should be interpreted

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 24

Page 171: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LISP Forms

I Evaluation of a form results in an s-expression� the internalrepresentation used by LISP

I Up to now we have not distinguished the underlyings-expression from the printed syntax humans use tocommunicate them

I Each type of form de�nes how the syntax should be interpreted

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 24

Page 172: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LISP Forms

I Evaluation of a form results in an s-expression� the internalrepresentation used by LISP

I Up to now we have not distinguished the underlyings-expression from the printed syntax humans use tocommunicate them

I Each type of form de�nes how the syntax should be interpreted

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 24

Page 173: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of Simple Forms

Form Evaluation Internal S-expr

variableLookup variable namein the variablesymbol table

age; 5

numberConvert ASCIIdigits to internalnumeric representation

15;00001111

constant

Lookup string andconvert to internedvalue, or create a new con-stant

'fred ;constant125

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 25

Page 174: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Compound Forms: The Function

I One common compound form is the function call

I To evaluate a function call of the form: (F1 F2 F3 . . . FN)

1. Lookup the �rst argument "F1" in the function symboltable and retrieve its implementation

2. Evaluate the remaining arguments(A recursive evaluation of each of the forms F2 . . . FN)

3. Apply the retrieved implementation to the arguments

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 26

Page 175: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Compound Forms: The Function

I One common compound form is the function call

I To evaluate a function call of the form: (F1 F2 F3 . . . FN)

1. Lookup the �rst argument "F1" in the function symboltable and retrieve its implementation

2. Evaluate the remaining arguments(A recursive evaluation of each of the forms F2 . . . FN)

3. Apply the retrieved implementation to the arguments

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 26

Page 176: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Compound Forms: The Function

I One common compound form is the function call

I To evaluate a function call of the form: (F1 F2 F3 . . . FN)

1. Lookup the �rst argument "F1" in the function symboltable and retrieve its implementation

2. Evaluate the remaining arguments(A recursive evaluation of each of the forms F2 . . . FN)

3. Apply the retrieved implementation to the arguments

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 26

Page 177: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Compound Forms: The Function

I One common compound form is the function call

I To evaluate a function call of the form: (F1 F2 F3 . . . FN)

1. Lookup the �rst argument "F1" in the function symboltable and retrieve its implementation

2. Evaluate the remaining arguments(A recursive evaluation of each of the forms F2 . . . FN)

3. Apply the retrieved implementation to the arguments

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 26

Page 178: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6APPLY <cons-implementation> to 5 6

→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 179: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)

LOOKUP CONS ; <cons-implementation>{defined internally by LISP}

EVAL aLOOKUP a ; 5

EVAL bLOOKUP b ; 6

APPLY <cons-implementation> to 5 6→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 180: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}

EVAL aLOOKUP a ; 5

EVAL bLOOKUP b ; 6

APPLY <cons-implementation> to 5 6→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 181: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6APPLY <cons-implementation> to 5 6

→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 182: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5

EVAL bLOOKUP b ; 6

APPLY <cons-implementation> to 5 6→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 183: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6APPLY <cons-implementation> to 5 6

→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 184: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6

APPLY <cons-implementation> to 5 6→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 185: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6APPLY <cons-implementation> to 5 6

→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 186: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Functions Example:

(SETQ a 5)(SETQ b 6)(CONS a b)

EVAL (CONS a b)LOOKUP CONS ; <cons-implementation>

{defined internally by LISP}EVAL a

LOOKUP a ; 5EVAL b

LOOKUP b ; 6APPLY <cons-implementation> to 5 6

→ (5 . 6)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 27

Page 187: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Compound Forms: Special Forms

I LISP de�nes a variety of special forms that de�ne their ownspecial evaluation rules

I What would happen if we evaluated (SETQ a (+ 1 1))like a function?

I A standard evaluation would:

I Lookup SETQ's implementationI Then evaluate the arguments.I When we evaluate 'a' we would get "ERROR! 'a'

undefined!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 28

Page 188: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Compound Forms: Special Forms

I LISP de�nes a variety of special forms that de�ne their ownspecial evaluation rules

I What would happen if we evaluated (SETQ a (+ 1 1))like a function?

I A standard evaluation would:

I Lookup SETQ's implementationI Then evaluate the arguments.I When we evaluate 'a' we would get "ERROR! 'a'

undefined!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 28

Page 189: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Compound Forms: Special Forms

I LISP de�nes a variety of special forms that de�ne their ownspecial evaluation rules

I What would happen if we evaluated (SETQ a (+ 1 1))like a function?

I A standard evaluation would:

I Lookup SETQ's implementationI Then evaluate the arguments.I When we evaluate 'a' we would get "ERROR! 'a'

undefined!"

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 28

Page 190: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Compound Forms: Special Forms II

I Actual evalution rule for (SETQ a (+ 1 1)) is a special form

I Unlike functions, LISP must not evaluate the second argument"a" as it is not de�ned!

I LISP must evaluate the third argument to know what valueshould be assigned to "a"

I LISP then stores a value for 'a' in a local symbol table

I LISP allows you to de�ne your own special forms with uniqueevaluation rules

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 29

Page 191: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of Compound Forms: Special Forms II

I Actual evalution rule for (SETQ a (+ 1 1)) is a special form

I Unlike functions, LISP must not evaluate the second argument"a" as it is not de�ned!

I LISP must evaluate the third argument to know what valueshould be assigned to "a"

I LISP then stores a value for 'a' in a local symbol table

I LISP allows you to de�ne your own special forms with uniqueevaluation rules

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 29

Page 192: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)

(OR (> a (+ 2 1)) (> a 2) )LOOKUP OR - a special form!!

EVAL (> a (+ 2 1))LOOKUP >

EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 193: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 194: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!

EVAL (> a (+ 2 1))LOOKUP >

EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 195: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 196: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >

EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 197: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5

EVAL (+ 2 1)LOOKUP +

EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 198: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 199: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +

EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 200: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2

EVAL 1 → 1→3

→ T→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 201: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 202: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3

→ T→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 203: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 204: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T

; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 205: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluation of OR Special Form

(SET Q a 5)(OR (> a (+ 2 1)) (> a 2) )

LOOKUP OR - a special form!!EVAL (> a (+ 2 1))

LOOKUP >EVAL a →5EVAL (+ 2 1)

LOOKUP +EVAL 2 → 2EVAL 1 → 1

→3→ T

→T ; stops on first "true" argument

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 30

Page 206: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

De�ning Your Own Functions

I Until now, we have only used built-in FUNCTIONS .(Eg: CAR, NULL, +, . . . )

I New functions are de�ned using the LAMBDA (λ) special form:

(lambda-keyword parameter-list function-body-form)

I The term LAMBDA comes from a mathematical theory ofcomputation developed by Alonzo Church in the 1930's!

I We'll skip the theory for now and plunge into usingLAMBDA's to create our own functions

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 31

Page 207: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

De�ning Your Own Functions

I Until now, we have only used built-in FUNCTIONS .(Eg: CAR, NULL, +, . . . )

I New functions are de�ned using the LAMBDA (λ) special form:

(lambda-keyword parameter-list function-body-form)

I The term LAMBDA comes from a mathematical theory ofcomputation developed by Alonzo Church in the 1930's!

I We'll skip the theory for now and plunge into usingLAMBDA's to create our own functions

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 31

Page 208: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

De�ning Your Own Functions

I Until now, we have only used built-in FUNCTIONS .(Eg: CAR, NULL, +, . . . )

I New functions are de�ned using the LAMBDA (λ) special form:

(lambda-keyword parameter-list function-body-form)

I The term LAMBDA comes from a mathematical theory ofcomputation developed by Alonzo Church in the 1930's!

I We'll skip the theory for now and plunge into usingLAMBDA's to create our own functions

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 31

Page 209: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

De�ning Your Own Functions

I Until now, we have only used built-in FUNCTIONS .(Eg: CAR, NULL, +, . . . )

I New functions are de�ned using the LAMBDA (λ) special form:

(lambda-keyword parameter-list function-body-form)

I The term LAMBDA comes from a mathematical theory ofcomputation developed by Alonzo Church in the 1930's!

I We'll skip the theory for now and plunge into usingLAMBDA's to create our own functions

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 31

Page 210: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Simple Example of a LAMBDA Expression

I Here is an example de�ning a two-argument mathematicalfunction

( LAMBDA︸ ︷︷ ︸keyword

(X Y)︸ ︷︷ ︸parameters

(+ X (* 2 Y))︸ ︷︷ ︸body form

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 32

Page 211: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Comparison of Procedures with λ-Expressions

I (LAMBDA (X Y) (PLUS X (TIMES 2 Y)))is a FUNCTION

I Mathematically, it is a mapping:

R× R→ R

I What would a traditional procedure with the same intent looklike?

double foo( double x, double y) {return x + 2 * y;

}

I Notice that the λ-expression does not de�ne a function nameor the types of its parameters

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 33

Page 212: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Comparison of Procedures with λ-Expressions

I (LAMBDA (X Y) (PLUS X (TIMES 2 Y)))is a FUNCTION

I Mathematically, it is a mapping:

R× R→ R

I What would a traditional procedure with the same intent looklike?

double foo( double x, double y) {return x + 2 * y;

}

I Notice that the λ-expression does not de�ne a function nameor the types of its parameters

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 33

Page 213: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Comparison of Procedures with λ-Expressions

I (LAMBDA (X Y) (PLUS X (TIMES 2 Y)))is a FUNCTION

I Mathematically, it is a mapping:

R× R→ R

I What would a traditional procedure with the same intent looklike?

double foo( double x, double y) {return x + 2 * y;

}

I Notice that the λ-expression does not de�ne a function nameor the types of its parameters

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 33

Page 214: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Comparison of Procedures with λ-Expressions

I (LAMBDA (X Y) (PLUS X (TIMES 2 Y)))is a FUNCTION

I Mathematically, it is a mapping:

R× R→ R

I What would a traditional procedure with the same intent looklike?

double foo( double x, double y) {return x + 2 * y;

}

I Notice that the λ-expression does not de�ne a function nameor the types of its parameters

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 33

Page 215: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Comparison of Procedures with λ-Expressions

I (LAMBDA (X Y) (PLUS X (TIMES 2 Y)))is a FUNCTION

I Mathematically, it is a mapping:

R× R→ R

I What would a traditional procedure with the same intent looklike?

double foo( double x, double y) {return x + 2 * y;

}

I Notice that the λ-expression does not de�ne a function nameor the types of its parameters

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 33

Page 216: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using λ-Expressions

I Evaluation of a LAMBDA returns an anonymous procedure

I It can be created where it is needed and need not be stored ina symbol table

I λ-expression can be used anywhere you would use a functionsymbol.

I How would I apply (LAMBDA (X Y) (+ X (* 2 Y)) ) to thearguments 4 and 15?

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 34

Page 217: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using λ-Expressions

I Evaluation of a LAMBDA returns an anonymous procedure

I It can be created where it is needed and need not be stored ina symbol table

I λ-expression can be used anywhere you would use a functionsymbol.

I How would I apply (LAMBDA (X Y) (+ X (* 2 Y)) ) to thearguments 4 and 15?

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 34

Page 218: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using λ-Expressions

I Evaluation of a LAMBDA returns an anonymous procedure

I It can be created where it is needed and need not be stored ina symbol table

I λ-expression can be used anywhere you would use a functionsymbol.

I How would I apply (LAMBDA (X Y) (+ X (* 2 Y)) ) to thearguments 4 and 15?

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 34

Page 219: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using λ-Expressions

I Evaluation of a LAMBDA returns an anonymous procedure

I It can be created where it is needed and need not be stored ina symbol table

I λ-expression can be used anywhere you would use a functionsymbol.

I How would I apply (LAMBDA (X Y) (+ X (* 2 Y)) ) to thearguments 4 and 15?

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 34

Page 220: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using λ-Expressions

I Evaluation of a LAMBDA returns an anonymous procedure

I It can be created where it is needed and need not be stored ina symbol table

I λ-expression can be used anywhere you would use a functionsymbol.

I How would I apply (LAMBDA (X Y) (+ X (* 2 Y)) ) to thearguments 4 and 15?

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 34

Page 221: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )

Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 222: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environment

Within the env, assign X ← 4, Y← 15Within the env, evaluate: (+ X (* 2 Y)):

EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 223: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 224: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):

EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 225: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]

; + of EVAL[X] and EVAL[(* 2 Y)]EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 226: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 227: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4

EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 228: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]

; * of EVAL[2]and EVAL[Y]EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 229: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 230: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2

EVAL[Y] ; 15; 30

; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 231: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 232: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30

; 34

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 233: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Evaluating Forms with λ-Expressions

Evaluating:

( (LAMBDA (X Y) (+ X (* 2 Y)) ) 4 15 )Create a new environmentWithin the env, assign X ← 4, Y← 15

Within the env, evaluate: (+ X (* 2 Y)):EVAL[(+ X (* 2 Y))]; + of EVAL[X] and EVAL[(* 2 Y)]

EVAL[X] ; 4EVAL[(* 2 Y)]; * of EVAL[2]and EVAL[Y]

EVAL[2] ; 2EVAL[Y] ; 15

; 30; 34Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 35

Page 234: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Summary of λ-expressions

I A LAMBDA Expression is: a list with 3 elements:

(LAMBDA (a1 a2 . . . an) <form>) where ai are atoms

I A λ-expression is used in the following form:

( (LAMBDA (a1 a2 . . . an) <form>) v1 v2 . . . vn)

I Each vi is a value or argument.

I Evaluation:

1. Create a new environment2. Bind each argument ai to its corresponding value vi3. Evaluate <form> in the environment.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 36

Page 235: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Summary of λ-expressions

I A LAMBDA Expression is: a list with 3 elements:

(LAMBDA (a1 a2 . . . an) <form>) where ai are atoms

I A λ-expression is used in the following form:

( (LAMBDA (a1 a2 . . . an) <form>) v1 v2 . . . vn)

I Each vi is a value or argument.

I Evaluation:

1. Create a new environment2. Bind each argument ai to its corresponding value vi3. Evaluate <form> in the environment.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 36

Page 236: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Summary of λ-expressions

I A LAMBDA Expression is: a list with 3 elements:

(LAMBDA (a1 a2 . . . an) <form>) where ai are atoms

I A λ-expression is used in the following form:

( (LAMBDA (a1 a2 . . . an) <form>) v1 v2 . . . vn)

I Each vi is a value or argument.

I Evaluation:

1. Create a new environment2. Bind each argument ai to its corresponding value vi3. Evaluate <form> in the environment.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 36

Page 237: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environment

Assign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 238: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)

Evaluate: (CONS (CAR X) Y):Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 239: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 240: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONS

EVAL[(CAR X)]EVAL X;(A B)

; AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 241: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 242: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B)

; AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 243: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); A

EVAL[Y] ; (C); (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 244: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 245: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Another Example of λ Evaluation

Evaluate: ( (LAMBDA (X Y) (CONS (CAR X) Y)) '(A B) '(C) ):

Create a new environmentAssign X←(A B) and Y←(C)Evaluate: (CONS (CAR X) Y):

Lookup CONSEVAL[(CAR X)]

EVAL X;(A B); AEVAL[Y] ; (C)

; (A C)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 37

Page 246: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 247: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 248: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 249: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 250: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 251: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 252: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Functions vs. Forms

Functions are abstract mathematical objects with someimplementation.

I Applying the function CAR to (A B C) results in A

I There are two types of functions:I Primitives: (CONS, ATOM, . . . )I User-de�ned: (( LAMBDA (v1 v2 . . . vn) <form>) e1 e2

. . .en)

Forms are syntactic expressions which get evaluated in a context

I "(+ X 5)" is a form that applies the function + to X and 5

I "(CAR X)" is a form that applies the function CAR to X

I "( (LAMBDA (X) X) 7)" is a form which applies λ to 7

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 38

Page 253: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→

(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 254: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)

( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 255: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→

(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 256: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)

( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 257: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→

A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 258: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A

( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 259: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→

CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 260: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS

( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 261: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→

LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 262: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA

( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 263: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→

undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 264: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of λ-Forms

( (LAMBDA (X Y) (CONS X (CDR (CDR Y))) 'A '(B C D) )→(A D)( (LAMBDA (X Y) (CONS X '(CDR Y)) 'A '(B C D) )→(A CDR Y)( (LAMBDA (X) (CAR X)) (CONS 'A NIL) )→A( (LAMBDA (X) (CAR X)) '(CONS 'A NIL) )→CONS( (LAMBDA (X) (CAR X)) '(LAMBDA (X) (CAR X)) )→LAMBDA( (LAMBDA (X) (CAR X)) (LAMBDA (X) (CAR X)) )→ undefined --- "LAMBDA" is not function

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 39

Page 265: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 266: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →

3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 267: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 268: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→

10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 269: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 270: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→

6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 271: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 272: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ected

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 273: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The LET Operator

I The LET operator creates a new environment with localbindings in it

(SETQ X 3)X →3

(LET ((X 5)(Y 2))

(* 2 X) )→10

(* 2 X)→6

I Operations within the LET use the local values

I Variables outside of the LET are una�ectedDr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 40

Page 274: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Relationship Between LET & λ

I The general form of the LET form is:

(LET ( (v1 e1) . . . (vn en) ) 〈form〉 )

I It is equivalent to the following λ - form:

((LAMBDA (v1 . . . vn) 〈form〉 ) e1 . . . en)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 41

Page 275: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Relationship Between LET & λ

I The general form of the LET form is:

(LET ( (v1 e1) . . . (vn en) ) 〈form〉 )

I It is equivalent to the following λ - form:

((LAMBDA (v1 . . . vn) 〈form〉 ) e1 . . . en)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 41

Page 276: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))

→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 277: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)

( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 278: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))

→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 279: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)

(LET ( (X 'A)(Y '(B C )))

(LET (( Y 2 ))(CONS X Y) ))

→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 280: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))

→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 281: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)

(LET ( (X (CONS (QUOTE A) NIL) ) )(CAR X) )

→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 282: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )

→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 283: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LET and λ

(LET ((X 'A)(Y '(B C)))

(CONS X Y))→(A B C)( (LAMBDA (X Y) (CONS X Y)) 'A '(B C))→(A B C)(LET ( (X 'A)

(Y '(B C )))(LET (( Y 2 ))

(CONS X Y) ))→(A . 2)(LET ( (X (CONS (QUOTE A) NIL) ) )

(CAR X) )→A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 42

Page 284: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Quote Special Form

I Special forms exercise control over the evaluation of theirarguments

I We have seen the "QUOTE" form

I Abbreviated to 'x, but it can be written (QUOTE X)

I Unlike function-call forms, QUOTE performs no evaluation ofarguments

I QUOTE is a "form" as it de�nes a way of interpreting asyntactic expression

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 43

Page 285: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Quote Special Form

I Special forms exercise control over the evaluation of theirarguments

I We have seen the "QUOTE" form

I Abbreviated to 'x, but it can be written (QUOTE X)

I Unlike function-call forms, QUOTE performs no evaluation ofarguments

I QUOTE is a "form" as it de�nes a way of interpreting asyntactic expression

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 43

Page 286: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Quote Special Form

I Special forms exercise control over the evaluation of theirarguments

I We have seen the "QUOTE" form

I Abbreviated to 'x, but it can be written (QUOTE X)

I Unlike function-call forms, QUOTE performs no evaluation ofarguments

I QUOTE is a "form" as it de�nes a way of interpreting asyntactic expression

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 43

Page 287: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Quote Special Form

I Special forms exercise control over the evaluation of theirarguments

I We have seen the "QUOTE" form

I Abbreviated to 'x, but it can be written (QUOTE X)

I Unlike function-call forms, QUOTE performs no evaluation ofarguments

I QUOTE is a "form" as it de�nes a way of interpreting asyntactic expression

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 43

Page 288: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

The Quote Special Form

I Special forms exercise control over the evaluation of theirarguments

I We have seen the "QUOTE" form

I Abbreviated to 'x, but it can be written (QUOTE X)

I Unlike function-call forms, QUOTE performs no evaluation ofarguments

I QUOTE is a "form" as it de�nes a way of interpreting asyntactic expression

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 43

Page 289: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B

→5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 290: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 291: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)

→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 292: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 293: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C

→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 294: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 295: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)

→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 296: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 297: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)

→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 298: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 299: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))

→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 300: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 301: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))

→'A

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 302: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of the QUOTE Form

(SETQ B 5)B →5

(QUOTE B)→B

C→"Error Undefined"

(QUOTE C)→C

(+ 1 2)→3

(QUOTE (+ 1 2))→(+ 1 2)

(QUOTE (QUOTE A))→'ADr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 44

Page 303: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 304: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 305: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 306: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))

→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 307: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 308: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 309: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))

→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 310: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LIST Form

I The use of QUOTE and LIST are sometimes confused

I The LIST function

1. evaluates each of its arguments2. concatenates results into a list

I A simple example:

(LIST (+ 1 2) (+ 3 4) (cons 'a 'b))→( 3 7 (a . b) )

I Could use QUOTE to prevent evaluation of LIST's arguments:

(LIST '(+ 1 2) '(+ 3 4) '(cons 'a 'b))→( (+ 1 2) (+ 3 4) (cons 'a 'b) )

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 45

Page 311: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →

(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 312: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 313: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →

(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 314: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 315: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) →

(PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 316: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 317: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) →

((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 318: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 319: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →

(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 320: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 321: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →

A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 322: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 323: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →

(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 324: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of LIST and QUOTE

(LIST t 5) →(t 5)

(LIST (QUOTE BAC)) →(BAC)

(LIST 'PLUS 3 4) → (PLUS 3 4)

(LIST '(PLUS 3 4)) → ((PLUS 3 4))

(LIST (PLUS 3 4)) →(7)

(CAR (LIST 'A 'B)) →A

(CDR (LIST 'A 'B)) →(B)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 46

Page 325: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 326: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))

→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 327: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 328: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)

→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 329: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 330: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)

→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 331: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 332: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using LIST to Create λ- Forms

I These expressions createλ-forms:

(LIST 'LAMBDA '(x) (LIST 'CAR 'x))→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CAR)→(LAMBDA (x) (CAR x))

((LAMBDA (fn) (LIST 'LAMBDA '(x) (LIST fn 'x))) 'CDR)→(LAMBDA (x) (CDR x))

I This is just a three element list. We'll explain how to use itbelow.

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 47

Page 333: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of COND

I Can write �If x > 0 then x else −x.� as:

(COND ((> X 0) x )( t (- x)) )

I Note the use of the constant t here to represent the alwaystrue condition

I Suppose we wish to write a "lookup" function:

(lambda (name)(COND ( (EQ name 'bob ) 'id321)

( (EQ name 'russ) 'id452)( (EQ name 'lisa) 'id621)( t 'unknown) ))

I Again, the constant t represents a default action

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 48

Page 334: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of COND

I Can write �If x > 0 then x else −x.� as:

(COND ((> X 0) x )( t (- x)) )

I Note the use of the constant t here to represent the alwaystrue condition

I Suppose we wish to write a "lookup" function:

(lambda (name)(COND ( (EQ name 'bob ) 'id321)

( (EQ name 'russ) 'id452)( (EQ name 'lisa) 'id621)( t 'unknown) ))

I Again, the constant t represents a default action

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 48

Page 335: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of COND

I Can write �If x > 0 then x else −x.� as:

(COND ((> X 0) x )( t (- x)) )

I Note the use of the constant t here to represent the alwaystrue condition

I Suppose we wish to write a "lookup" function:

(lambda (name)(COND ( (EQ name 'bob ) 'id321)

( (EQ name 'russ) 'id452)( (EQ name 'lisa) 'id621)( t 'unknown) ))

I Again, the constant t represents a default action

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 48

Page 336: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Examples of COND

I Can write �If x > 0 then x else −x.� as:

(COND ((> X 0) x )( t (- x)) )

I Note the use of the constant t here to represent the alwaystrue condition

I Suppose we wish to write a "lookup" function:

(lambda (name)(COND ( (EQ name 'bob ) 'id321)

( (EQ name 'russ) 'id452)( (EQ name 'lisa) 'id621)( t 'unknown) ))

I Again, the constant t represents a default actionDr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 48

Page 337: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function? No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 338: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function? No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 339: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function? No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 340: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function? No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 341: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function?

No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 342: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Conditional Forms: COND

I The general form of the COND expression is:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I Check conditions sequentially until one succeeds

I Evaluate corresponding form and return

I If no condition succceeds, COND results in nil

I Is COND a function? No - only partially evaluated

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 49

Page 343: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs. the Procedural IF Statement

I The "COND" expression:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I The equivalent procedural "IF"

IF CONDITION-1 THENFORM-1

ELSEIF CONDITION-2FORM-2

ELSEIF CONDITION-NFORM-N

END

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 50

Page 344: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs. the Procedural IF Statement

I The "COND" expression:

(COND (CONDITION-1 FORM-1)(CONDITION-2 FORM-2)...(CONDITION-N FORM-N) )

I The equivalent procedural "IF"

IF CONDITION-1 THENFORM-1

ELSEIF CONDITION-2FORM-2

ELSEIF CONDITION-NFORM-N

ENDDr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 50

Page 345: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs C "?" Macro

I LISP COND function is closer C's condition-value macro, "?"

I The COND is conditional valued function, and not a controlstructure

I In C, one can write: y = (x > 0) ? x : -x

I This is, of course, the ABS function we de�ned earlier:

(COND ((> X 0) x )( t (- x)) )

I Can't ask the value of a procedural "IF" statement

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 51

Page 346: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs C "?" Macro

I LISP COND function is closer C's condition-value macro, "?"

I The COND is conditional valued function, and not a controlstructure

I In C, one can write: y = (x > 0) ? x : -x

I This is, of course, the ABS function we de�ned earlier:

(COND ((> X 0) x )( t (- x)) )

I Can't ask the value of a procedural "IF" statement

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 51

Page 347: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs C "?" Macro

I LISP COND function is closer C's condition-value macro, "?"

I The COND is conditional valued function, and not a controlstructure

I In C, one can write: y = (x > 0) ? x : -x

I This is, of course, the ABS function we de�ned earlier:

(COND ((> X 0) x )( t (- x)) )

I Can't ask the value of a procedural "IF" statement

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 51

Page 348: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs C "?" Macro

I LISP COND function is closer C's condition-value macro, "?"

I The COND is conditional valued function, and not a controlstructure

I In C, one can write: y = (x > 0) ? x : -x

I This is, of course, the ABS function we de�ned earlier:

(COND ((> X 0) x )( t (- x)) )

I Can't ask the value of a procedural "IF" statement

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 51

Page 349: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

COND vs C "?" Macro

I LISP COND function is closer C's condition-value macro, "?"

I The COND is conditional valued function, and not a controlstructure

I In C, one can write: y = (x > 0) ? x : -x

I This is, of course, the ABS function we de�ned earlier:

(COND ((> X 0) x )( t (- x)) )

I Can't ask the value of a procedural "IF" statement

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 51

Page 350: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND I

I Only code for satis�ed conditions is executed

(LAMBDA (x y)(COND ( (= y 0) 'error)

( t (\ x y))))

(apply λ '(10 2)) → 2(apply λ '(10 0)) → 'error

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 52

Page 351: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND I

I Only code for satis�ed conditions is executed

(LAMBDA (x y)(COND ( (= y 0) 'error)

( t (\ x y))))

(apply λ '(10 2)) →

2(apply λ '(10 0)) → 'error

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 52

Page 352: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND I

I Only code for satis�ed conditions is executed

(LAMBDA (x y)(COND ( (= y 0) 'error)

( t (\ x y))))

(apply λ '(10 2)) → 2

(apply λ '(10 0)) → 'error

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 52

Page 353: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND I

I Only code for satis�ed conditions is executed

(LAMBDA (x y)(COND ( (= y 0) 'error)

( t (\ x y))))

(apply λ '(10 2)) → 2(apply λ '(10 0)) →

'error

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 52

Page 354: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND I

I Only code for satis�ed conditions is executed

(LAMBDA (x y)(COND ( (= y 0) 'error)

( t (\ x y))))

(apply λ '(10 2)) → 2(apply λ '(10 0)) → 'error

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 52

Page 355: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →

5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 356: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5

(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 357: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) →

nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 358: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil

(COND (t 5)(t 6)) → 5

(COND (nil 5)(t 6)) →6

(COND (nil 3)(t 1)) →1

(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 359: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) →

5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 360: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5

(COND (nil 5)(t 6)) →6

(COND (nil 3)(t 1)) →1

(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 361: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →

6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 362: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6

(COND (nil 3)(t 1)) →1

(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 363: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →

1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 364: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1

(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 365: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) →

Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 366: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More examples of COND II

(COND (t 5)) →5(COND (nil 5)) → nil(COND (t 5)

(t 6)) → 5(COND (nil 5)

(t 6)) →6(COND (nil 3)

(t 1)) →1(COND nil) → Error 'nil' should be a list

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 53

Page 367: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 368: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 369: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 370: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 371: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) →

T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 372: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T

(and (not (= y 0)) (\ x y)) →X/Y if y 6=0 otherwise nil

(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 373: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 374: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil

(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 375: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) →

T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 376: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T

(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 377: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) →

6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 378: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6

(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 379: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) →

NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 380: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Boolean operators: and and or are implemented as specialforms

I Value is value of last argument evaluated

I Both and and or implement short-circuiting(evaluate only enough arguments to determine truth value)

I The and special form

(and (< 6 4) (>= (/ 10 0) 0)) → T(and (not (= y 0)) (\ x y)) →

X/Y if y 6=0 otherwise nil(and (> 6 4) (>= 4 2)) → T(and 5 6) → 6(and t (cdr '(1))) → NIL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 54

Page 381: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

OR Special Forms

I The or special form

(or (= 3 9) (eq 'price 'price)) → T(or 5 6 7) → 5

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 55

Page 382: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

OR Special Forms

I The or special form

(or (= 3 9) (eq 'price 'price)) →

T(or 5 6 7) → 5

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 55

Page 383: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

OR Special Forms

I The or special form

(or (= 3 9) (eq 'price 'price)) → T

(or 5 6 7) → 5

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 55

Page 384: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

OR Special Forms

I The or special form

(or (= 3 9) (eq 'price 'price)) → T(or 5 6 7) →

5

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 55

Page 385: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

OR Special Forms

I The or special form

(or (= 3 9) (eq 'price 'price)) → T(or 5 6 7) → 5

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 55

Page 386: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) → t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 387: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) → t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 388: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))

( (LAMBDA (x) (COND (X nil)(t t))) nil) → t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 389: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) →

t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 390: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) → t

( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 391: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) → t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →

nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 392: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Boolean Special Forms

I Why are and and or special forms?

I They do not evaluates all arguments apriori

I De�ne your own version of the NULL predicate

(LAMBDA (x)(COND (X nil)

(t t)))( (LAMBDA (x) (COND (X nil)(t t))) nil) → t( (LAMBDA (x) (COND (X nil)(t t))) '(1 2 3)) →nil

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 56

Page 393: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george →

Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 394: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable

(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 395: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) →

nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 396: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil

(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 397: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) →

nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 398: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil

(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 399: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) →

t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 400: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t

(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 401: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→

t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 402: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t

(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 403: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) →

t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 404: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t

(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 405: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) →

t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 406: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of AND and OR

george → Error: undefined variable(and t nil) → nil(and t nil george) → nil(or t nil) → t(or t nil george)→ t(and (atom 'tom) (null nil)) → t(or (atom '(a b)) (atom 'fred)) → t

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 57

Page 407: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 408: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 409: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t

(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 410: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→

george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 411: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george

(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 412: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→

fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 413: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred

(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 414: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→

(5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 415: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)

((lambda (m)(or (+ m 1) (= m 0)) ) 0 )

→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 416: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→

1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 417: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

And More Examples of AND and OR

(or (and (listp 'tom) (atom 'tom))(or (atom '(a b)) (listp '(a))) )

≡(or (and nil t)(or nil t) )

→ t(and 'fred 'george)→ george(or 'fred 'george)→ fred(or nil (list 5 4))→ (5 4)((lambda (m)

(or (+ m 1) (= m 0)) ) 0 )→ 1 ; Note: (+ m 1) has no side e�ect

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 58

Page 418: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Passing a Function as a Parameter

I Conceptually (THIS CODE WILL NOT EXECUTE IN LISP)

( (LAMBDA (fn)(fn 1 2)

) 'max )→

2( (LAMBDA (fn)

(fn 1 2)) '+ )→ 3

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 59

Page 419: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Passing a Function as a Parameter

I Conceptually (THIS CODE WILL NOT EXECUTE IN LISP)

( (LAMBDA (fn)(fn 1 2)

) 'max )→ 2

( (LAMBDA (fn)(fn 1 2)

) '+ )→ 3

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 59

Page 420: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Passing a Function as a Parameter

I Conceptually (THIS CODE WILL NOT EXECUTE IN LISP)

( (LAMBDA (fn)(fn 1 2)

) 'max )→ 2( (LAMBDA (fn)

(fn 1 2)) '+ )→

3

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 59

Page 421: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Passing a Function as a Parameter

I Conceptually (THIS CODE WILL NOT EXECUTE IN LISP)

( (LAMBDA (fn)(fn 1 2)

) 'max )→ 2( (LAMBDA (fn)

(fn 1 2)) '+ )→ 3

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 59

Page 422: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using FUNCALL in Lisp

I By convention, Lisp does not evaluate �rst argument (unless itis a LAMBDA)

I FUNCALL provides the necessary hack

( (LAMBDA (fn)(FUNCALL fn 1 2)

)'max )→ 2

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 60

Page 423: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using FUNCALL in Lisp

I By convention, Lisp does not evaluate �rst argument (unless itis a LAMBDA)

I FUNCALL provides the necessary hack

( (LAMBDA (fn)(FUNCALL fn 1 2)

)'max )→ 2

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 60

Page 424: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using FUNCALL in Lisp

I By convention, Lisp does not evaluate �rst argument (unless itis a LAMBDA)

I FUNCALL provides the necessary hack

( (LAMBDA (fn)(FUNCALL fn 1 2)

)'max )→

2

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 60

Page 425: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Using FUNCALL in Lisp

I By convention, Lisp does not evaluate �rst argument (unless itis a LAMBDA)

I FUNCALL provides the necessary hack

( (LAMBDA (fn)(FUNCALL fn 1 2)

)'max )→ 2

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 60

Page 426: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Naming Your Own Functions

( (LAMBDA (big)(FUNCALL big (FUNCALL big 1 2) (FUNCALL big 3 4) )

)

'(LAMBDA (X Y) (IF (> X Y) X Y)) ; what do I do?

)→ 4

I De�ne your function

I Pass to λ where bound to parameter 'big'

I Use 'big' repeatedly

I Evaluate the result!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 61

Page 427: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Naming Your Own Functions

( (LAMBDA (big)

(FUNCALL big (FUNCALL big 1 2) (FUNCALL big 3 4) )

)'(LAMBDA (X Y) (IF (> X Y) X Y)) ; what do I do?

)

→ 4

I De�ne your function

I Pass to λ where bound to parameter 'big'

I Use 'big' repeatedly

I Evaluate the result!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 61

Page 428: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Naming Your Own Functions

( (LAMBDA (big)(FUNCALL big (FUNCALL big 1 2) (FUNCALL big 3 4) )

)'(LAMBDA (X Y) (IF (> X Y) X Y)) ; what do I do?

)

→ 4

I De�ne your function

I Pass to λ where bound to parameter 'big'

I Use 'big' repeatedly

I Evaluate the result!

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 61

Page 429: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Naming Your Own Functions

( (LAMBDA (big)(FUNCALL big (FUNCALL big 1 2) (FUNCALL big 3 4) )

)'(LAMBDA (X Y) (IF (> X Y) X Y)) ; what do I do?

)→

4

I De�ne your function

I Pass to λ where bound to parameter 'big'

I Use 'big' repeatedly

I Evaluate the result!Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 61

Page 430: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Naming Your Own Functions

( (LAMBDA (big)(FUNCALL big (FUNCALL big 1 2) (FUNCALL big 3 4) )

)'(LAMBDA (X Y) (IF (> X Y) X Y)) ; what do I do?

)→ 4

I De�ne your function

I Pass to λ where bound to parameter 'big'

I Use 'big' repeatedly

I Evaluate the result!Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 61

Page 431: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Named Functions with FLET

I A convenient short-form for naming functions

(FLET ( (big (x y) (IF (> x y) x y)) )(big

(big 1 2)(big 3 4) )

))→

4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 62

Page 432: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Named Functions with FLET

I A convenient short-form for naming functions

(FLET ( (big (x y) (IF (> x y) x y)) )(big

(big 1 2)(big 3 4) )

))→ 4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 62

Page 433: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Multiple Functions with FLET

I List as many functions as you like

(FLET ( (big (x y) (IF (> x y) x y))(sum (x y) (+ x y)))

(big(sum 1 2)(sum 3 4) )

))→

7

I Writing helper functions simpli�es code and makes it moretransparent

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 63

Page 434: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Multiple Functions with FLET

I List as many functions as you like

(FLET ( (big (x y) (IF (> x y) x y))(sum (x y) (+ x y)))

(big(sum 1 2)(sum 3 4) )

))→ 7

I Writing helper functions simpli�es code and makes it moretransparent

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 63

Page 435: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Multiple Functions with FLET

I List as many functions as you like

(FLET ( (big (x y) (IF (> x y) x y))(sum (x y) (+ x y)))

(big(sum 1 2)(sum 3 4) )

))→ 7

I Writing helper functions simpli�es code and makes it moretransparent

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 63

Page 436: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Co-reference with FLET

I What does this return?

(FLET ( (square (x) (* x x))(sos (x y) (+ (square x) (square y)))

)(sos 3 4))

)→

25

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 64

Page 437: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Co-reference with FLET

I What does this return?

(FLET ( (square (x) (* x x))(sos (x y) (+ (square x) (square y)))

)(sos 3 4))

)→ 25

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 64

Page 438: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 439: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→

7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 440: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 441: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 442: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )

→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 443: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→

-4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 444: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

Local Shadowing of Global Functions

I Can also rede�ne system functions locally

(FLET ( (max (x y) (+ x y)))

(max 3 4) )→ 7

I Rede�ne local functions in terms of system de�ntion

(FLET ( (max (x y) (- (max x y))))

(max 3 4) )→ -4

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 65

Page 445: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)

(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ (

(A) ((T T)) (5) (A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 446: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)

(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A)

((T T)) (5) (A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 447: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)

(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A)

((T T)) (5) (A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 448: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)

(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T))

(5) (A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 449: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)

(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T))

(5) (A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 450: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)

(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5)

(A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 451: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)

(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5)

(A B C D E F) (A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 452: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)

(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5) (A B C D E F)

(A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 453: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)

(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5) (A B C D E F)

(A B C) A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 454: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)

(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5) (A B C D E F) (A B C)

A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 455: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5) (A B C D E F) (A B C)

A

)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 456: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

More Examples of Functional Args

(FLET ((applier (x y fn) (FUNCALL fn x (CDR y))))(LIST

(applier 'a '(bcd) 'CONS)(applier '(t t) '(t) 'CONS)(applier '5 '(a) 'cons)(applier '(a b c) '(d e f) 'APPEND)(applier '(a b c) '(t) 'APPEND)(applier 'a '(b c) '(LAMBDA (x y) x))

))→ ( (A) ((T T)) (5) (A B C D E F) (A B C) A)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 66

Page 457: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

FLET Does Not Permit Self-Reference

I Here

(FLET ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1) )

→ ** Error: foo undefined

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 67

Page 458: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

FLET Does Not Permit Self-Reference

I Here

(FLET ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1) )

** Error: foo undefined

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 67

Page 459: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

FLET Does Not Permit Self-Reference

I Here

(FLET ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1) )

→ ** Error: foo undefined

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 67

Page 460: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LABELS Permits Self-Reference

I Here

(LABELS ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1))

→ 0

I Self-reference allows functional paradigm to computeanything!

I We will explore this at length in the next unit

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 68

Page 461: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LABELS Permits Self-Reference

I Here

(LABELS ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1))

0

I Self-reference allows functional paradigm to computeanything!

I We will explore this at length in the next unit

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 68

Page 462: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LABELS Permits Self-Reference

I Here

(LABELS ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1))

→ 0

I Self-reference allows functional paradigm to computeanything!

I We will explore this at length in the next unit

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 68

Page 463: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LABELS Permits Self-Reference

I Here

(LABELS ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1))

→ 0

I Self-reference allows functional paradigm to computeanything!

I We will explore this at length in the next unit

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 68

Page 464: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure Lisp

The LAMBDA Special FromFunctions vs. FormsThe LET OperatorQuote and List Special FormsLabeled Functions

LABELS Permits Self-Reference

I Here

(LABELS ( (foo (x) (IF (= x 0)0(foo (- x 1)) ))

)(foo 1))

→ 0

I Self-reference allows functional paradigm to computeanything!

I We will explore this at length in the next unit

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 68

Page 465: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM is

I Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 466: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, X

I Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 467: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)

I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 468: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)

I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 469: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 470: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION is

I Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 471: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION isI Primitive atomic: CONS, CAR, EQ, . . .

I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 472: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION isI Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)

I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 473: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION isI Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)

I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 474: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Summary of Pure Lisp

I A FORM isI Atom [constant or variable]: nil, 5, XI Function application: (fn f1 ... fn)I Quoted expr: (QUOTE s)I Cond expr:

(COND ((c1f1)...

(cnfn) ))

I A FUNCTION isI Primitive atomic: CONS, CAR, EQ, . . .I λ-expression: (LAMBDA (v1 ... vn) 〈form〉)I Variable (evaluating to Function)I Labeled λ-expression: FLET or LABEL

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 69

Page 475: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp I

I To facilitate marking of your code we ask you to be impure:

I Conceptually, de�ne global named functions in LispEnvironment with

(SETF f '(LAMBDA (x) (- x)))(FUNCALL f 1) → -1

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 70

Page 476: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp I

I To facilitate marking of your code we ask you to be impure:

I Conceptually, de�ne global named functions in LispEnvironment with

(SETF f '(LAMBDA (x) (- x)))(FUNCALL f 1) →

-1

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 70

Page 477: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp I

I To facilitate marking of your code we ask you to be impure:

I Conceptually, de�ne global named functions in LispEnvironment with

(SETF f '(LAMBDA (x) (- x)))(FUNCALL f 1) → -1

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 70

Page 478: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp II

I Short cut which also puts f into function space

(DEFUN f (x)(if (> x 0)

x-x))

(f 1)→1(f -1)→1

I Watch out for side e�ects (old de�nitions of functions lyingaround)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 71

Page 479: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp II

I Short cut which also puts f into function space

(DEFUN f (x)(if (> x 0)

x-x))

(f 1)→1

(f -1)→1

I Watch out for side e�ects (old de�nitions of functions lyingaround)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 71

Page 480: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp II

I Short cut which also puts f into function space

(DEFUN f (x)(if (> x 0)

x-x))

(f 1)→1(f -1)→1

I Watch out for side e�ects (old de�nitions of functions lyingaround)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 71

Page 481: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Impure Lisp II

I Short cut which also puts f into function space

(DEFUN f (x)(if (> x 0)

x-x))

(f 1)→1(f -1)→1

I Watch out for side e�ects (old de�nitions of functions lyingaround)

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 71

Page 482: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) →

16(applier '*) → 55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 483: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16

(applier '*) → 55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 484: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16(applier '*) →

55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 485: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16(applier '*) → 55

(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 486: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16(applier '*) → 55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→

18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 487: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16(applier '*) → 55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72

Page 488: webdocs.cs.ualberta.ca · Lisp Core Primitives Lisp rmsoF Summary of Pure Lisp Lisp History Symbolic Expressions Predicates Lisp History I History: LIS t P rocessing Speci ed by McCarthy

Lisp Core PrimitivesLisp FormsSummary of Pure LispReally Pure LispImpure Lisp

Functional Arguments & DEFUN

I Conceptually, we can also pass functions as arguments

(DEFUN applier (fn) (fn 5 11))

(applier '+) → 16(applier '*) → 55(applier '(LAMBDA (x y) (+ (* 2 x) (- y 3))) )→ 18

I THE ABOVE WILL NOT WORK IN LISP (ok in Scheme)Lisp does not evaluate its �rst argument. Write instead:

(DEFUN applier (fn) (funcall fn 1 2))

Dr. B. Price & Dr. R. Greiner CMPUT 325 - Lisp Basics 72


Top Related