webdocs.cs.ualberta.ca · lisp core primitives lisp rmsof summary of pure lisp lisp history...

Post on 24-Mar-2020

48 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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