introductory lisp programming lecture # 2 main topics –basic lisp data types –lisp primitives...

37
Introductory Lisp Programming Lecture # 2 Main Topics Basic Lisp data types Lisp primitives Details of list handling • Cons cells (boxes) & their representation in memory • List operators: cons, append, rplaca, rplacd.. • Lists as arguments to functions

Post on 20-Dec-2015

226 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Introductory Lisp Programming Lecture # 2

• Main Topics

– Basic Lisp data types

– Lisp primitives

– Details of list handling

• Cons cells (boxes) & their representation in memory

• List operators: cons, append, rplaca, rplacd..

• Lists as arguments to functions

Page 2: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Lisp Lecture #2

Exam #1 Will cover Lisp programming

February ???-th, take-home

• Today: Review lecture on Lisp

More on functions

More on Lisp internals

Non-destructive and destructive operations

• Next time: Problem solving using search (Chapter 3 R&N)

Read chapter before coming to class

Page 3: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Basic LISP Data Types

Expression

SequenceArrayAtom

List StringNumberSymbol

Integer Ratio Float

3 7/13 23.45

(has arms 2)“Rosebud”

‘Rosebud

(12 7 32 78)

(bob (has (arms 2) (legs 2)))

Structure

4, 7, 12..Thing id: 764 loc: (12 29)..

Page 4: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Truth values = T (true) and NIL (false)

AND, OR, NOT, XOR.. Everything but NIL has a truth value of T - even 0!!!

(and t t) => t; (and 1 0) => t ; (or nil nil 0) => t; (xor 1 t) => nil

(not t) =>nil; (not nil) => t; (not 45) => nil;

*Logical operators only evaluate as many arguments (left to right) as necessary to determine the truth value of the expression.

(and nil (* 88 99 66)) => nil ;; the * is never evaluated

(and (* 88 99) nil) => nil ;; the * IS evaluated due to L-R eval order

(not (and (or nil nil) (xor 1 0))) => t ;; the xor is never evaluated

* NIL is both false and the empty list, depending upon the context

Logical Operators

Page 5: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

a b

Dotted Pairs> (cons ‘a nil) => (a)> (cons ‘a (cons ‘b nil)) => (a b)> (cons ‘a ‘(b)) => (a b)> (cons ‘a ‘b) => (a . b)> (cons ‘(a b) ‘c) => ((a b) . c)> (cons (cons ‘a ‘b) ‘c) => ((a . b) . c)

a a b

a b

c

a b

c

> (car ‘(a . b)) => a> (cdr ‘(a . b)) => b ; symbol> (cdr ‘(a b)) => (b) ; cons cell

Page 6: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Recursion

• count-atoms

• null

• cond

Page 7: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Optional arguments

Binds to the car of list of arguments

Binds to the cdr of list of arguments

Optional argument

(root 4)==>2

(root 4 3)==>4 1/3

Page 8: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

More on Lisp Internals

• Storage allocation

• Non-destructive list operations

• Destructive list operations

• Pointer manipulation: setf, eq

• Efficiency considerations

Page 9: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Internal List Representation

• Free Storage List

Page 10: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• equal

• eq

Two pointers point to the same location

Page 11: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Garbage collection

Page 12: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Efficiency

• user-reverse

Page 13: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Efficiency Considerationsres is optional argument

Argument list

Page 14: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Variable Assignment & AccessAssignment SETF

(setf y (+ 3 5)) => y assigned the value 8

(setf y ‘((a 1) (b 2) (c 3))) => y assigned to the data list

* Can be used to set arbitrary locations in Lisp data structures

(setf (second y) 88) => 88

y => ((a 1) 88 (c 3))

SETQ

(setf x (+ 3 5)) => x assigned the value 8

* First argument must be a symbol, not a general location.

Access

Anytime a variable appears, it will be evaluated (i.e. its value will be accessed), unless the variable appears inside the scope of a single or double quote, or is an argument to a macro.

> x => 8

> ‘x => x

Page 15: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Evaluating LISP Expressions (1)

Evaluate := compute/fetch value of an expression

Form := an expression to be evaluated

A number evaluates to itself:

> 76 => 76

A variable evaluates to its value:

> (setf x 54)

> x => 54

A quoted symbol evaluates to the symbol itself:

> ‘z => z

A string evaluates to itself:

> “portland” => “portland”

Page 16: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Evaluating LISP Expressions (2)

A single quoted list evaluates to a simple list of symbols

> ‘(a b c) => (a b c)

> ‘(+ 2 3) => (+ 2 3)

An unquoted list evaluates to a function call

> (+ 2 3) => 5

> (a b c) => ERROR: attempt to call an undeclared function ‘a

Evaluate arguments left-to-right, then call function:

> (setf bob 5 bill 7 sue 9)

> (+ bob bill sue) => 21 ;; (Calls + with 3 values: 5, 7, 9)

Polish notation: (operator operand1 operand2 … operandn)

(+ 2 3 4 5) simpler than 2 + 3 + 4 + 5 + 6 if you can live with all the parentheses!!

Parentheses => no need for arithmetic precedence rules

> (+ (* a b) (* c d)) -vs- a*b + c*d

Page 17: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Depth-First Evaluation Order

• (f1 (f2 a (f3 b)) c 8 (f2 3 2))

f1

f2

a f3

b

c 8 f2

3 2

Page 18: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

List AccessorsLENGTH

(length ‘(a b c)) => 3; (length nil) => 0

FIRST (or CAR)

(first ‘(a b c)) => a ; (first y) => (a 1) ; (first ‘y) => Error!

REST (or CDR)

(rest ‘(a b c)) => (b c); (rest y) => ((b 2) (c 3));

(first (rest y)) => (b 2); (first (first (rest y))) => b

NTH

(nth 0 ‘(a b c)) => a ; (nth 2 ‘(a b c)) => c

NTHCDR (n nested calls to REST/CDR)

(nthcdr 2 y) => ((c 3))

BUTLAST (everything but the last n elements)

(butlast y 2) => ((a 1))

FIRST, SECOND, THIRD...TENTH

return corresponding element of the list

Data: y => ((a 1) (b 2) (c 3))

Page 19: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

List Constructors

CONS (“construct”)

(cons ‘a ‘(a b c)) => (a a b c); (cons ‘(a b) ‘(c d)) => ‘((a b) c d)

(cons ‘a nil) => (a); (cons nil ‘(a b)) => (nil a b)) = (() a b)

LIST

(list ‘a ‘b) => (a b); (list ‘(a b) ‘c ‘d) => ((a b) c d)

APPEND

(append ‘(a b c) ‘(d e f)) => (a b c d e f); (append nil ‘(a b)) => (a b)

These are not destructive:

> (setf x ‘(a b c))

> (cons 62 x) => (62 a b c)

> x => (a b c)

Page 20: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Execution of cons

Page 21: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation
Page 22: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Destructive List Modifiers (1)PUSH/POP: treat list like a stack and alter symbol values (“destructive”)

> (push 77 x) => (77 a b c)

> x => (77 a b c)

> (pop x) => 77

> x => (a b c)

REVERSE (non-destructive)

(reverse ‘(a b c)) => (c b a) ;

(reverse ‘((a 1) (b 2))) => ((b 2) (a 1))

NREVERSE (destructive reverse => no copying, but use setf for safety)

> (setf x '(a b c)) => (a b c)

> (nreverse x) => (c b a)

> x => (a)

> (setf x '(a b c)) => (a b c)

> (setf x (nreverse x)) => (c b a)

> x => (c b a)

Page 23: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

List Modifiers (2)

SETF + List Accessors (Destructive!):

> (setf y '(a b c)) => (a b c)

> (setf (second y) 23) => 23

> y => (a 23 c)

> (setf (second (cdr y)) 99) => 99

> y => (a 23 99)

> (setf (cdr y) '(aaa bbb ccc)) => (aaa bbb ccc)

> y => (a aaa bbb ccc)

Page 24: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Cons Cells > (setf x ‘(ntnu idi lade)) => (ntnu idi lade)> (setf y (cons ‘trondheim x)) => (trondheim ntnu idi lade)> (setf z (cons ‘(1 2 3) y)) => ( (1 2 3) trondheim ntnu idi lade)

x

= cons cell (standard lisp terminology) or box (Winston)

trondheim

y

1 32z

ntnu idi lade

*Each call to cons uses one new cons cell and sets car to first elem, cdr to 2nd.

Shared area

Page 25: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

List Creation > (setf x ‘(a b c)) ;; Uses 3 newcons cells ; same as (setf x (list ‘a ‘b ‘c))> (setf y ‘(1 2 3)) ;; Uses 3 more new cons cells; same as (setf y (list 1 2 3))> (setf z (list x y ‘the ‘end)) ;; Uses 4 more new cons cells

a b c

1 2 3

endthe

y

x

z

Page 26: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

Cons cells in memory

1-byte data type4-byte pointer

to cell’s value

4-byte pointer to next cons cell

Memory

FFF101

FFF101:

FFF252

FFF252:

ntnuvalue: unbound

idivalue: unbound

ladevalue: unbound

Symbol Table

AAA050:FFE073:

AAA250:

AAA150:

AAA050

AAA150

AAA250

000000

FFF849: CCC250

FFF849

CCC250: 639

Simple Data Storage*Cons cells, symbol tableand regular data storageare all in memory

(setf x ‘(ntnu idi lade 639))

xvalue: FFE073

*Each symbolhas many possible propertiesin the symbol table

Page 27: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Non-Destructive List Operations

Many beginners do this mistake

They think about variables as separate “containers”

Page 28: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

APPEND

ntnu idi lade

x

1 2 3

y

z

> (setf x ‘(ntnu idi lade))

> (setf y ‘(1 2 3))

> (setf z (append x y))> z (ntnu idi lade 1 2 3)> x (ntnu idi lade)

*Copies all of x’s cons cells, sox is unchanged.

copied

shared

Page 29: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• More on Destructive List Operations

NCONC is destructive operation!!!!!!

Page 30: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

• Destructive Lisp Operations

SETF is destructive operation!!!!!!

Page 31: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

SETF is can cause infinite printout loop!!!!!

Page 32: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

NCONC (Destructive Append)

ntnu idi lade

x

1 2 3

y

(setf x ‘(ntnu idi lade) y ‘(1 2 3))(setf z (nconc x y))> z(ntnu idi lade 1 2 3)> x (ntnu idi lade 1 2 3)

z

(nconc list1 list2…listn) - changes ‘next ptr in the last cons cells of list1 - listn-1

‘Destructive’ function => some of the original arguments may be changed

NCONC is destructive APPEND !!!!!

Page 33: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

RPLACA = (SETF (FIRST ..))SETF can make destructive modifications to lists.> (setf x ‘(a b c))> (setf y x)> (setf (first y) 99)> y (99 b c)> x (99 b c)> (setf (nth 2 y) 88)> x (99 b 88)> (rplaca y 55)> x (55 b 88)

a

b

c

x

y

99 88

55

RPLACA is destructive !!!!!

Page 34: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

RPLACD = (SETF (REST ..))

> (setf x ‘(a b c))> (setf y x)> (setf (rest y) ‘(22 33))> x (a 22 33)> (rplacd y ‘(77 88))> x (a 77 88)

ab c

x

y

22 33

77 88

*There are no remaining pointers to (b c) nor (22 33). Their cons cells should be freed up for cons and list to reuse. This is, theyshould be garbage collected. LISP hasautomatic garbage collection.

RPLACD is destructive !!!!!

Page 35: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

List Parameters in Function Calls (1)•LISP uses “call by value” for all parameters. •In the case of lists, this means that the formal parameters get a copy of the pointer to the list, not a copy of the whole list.

(defun change1 (list) (setf (first list) (first (last list))))

(defun change2 (list) (cons (first (last list)) (rest list)))

a b c

x

value:

change1

list: change2

list:

> (setf x ‘(a b c))

Page 36: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

a b c

List Parameters in Function Calls (2)

x

value:

> (setf x ‘(a b c)) > (change1 x)

* Change1 uses setf(first..), which is destructive. So x changes also.

c

change1

list:

SETF is destructive !!!!!

(defun change1 (list) (setf (first list) (first (last list))))

(defun change2 (list) (cons (first (last list)) (rest list)))

Page 37: Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation

a b c

x

value:

change2

list:

c

List Parameters in Function Calls (3)

> (setf x ‘(a b c))> (change2 x)* Change2 uses cons, which is non-destructive. So x is unchanged.

(defun change1 (list) (setf (first list) (first (last list))))

(defun change2 (list) (cons (first (last list)) (rest list)))

CONS is not destructive !!!!!