David Evans cs.virginia/~evans

28
David Evans http://www.cs.virginia.edu/ ~evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Con’s car cdr sdr wdr

description

Lecture 8: Con’s car cdr sdr wdr. David Evans http://www.cs.virginia.edu/~evans. CS200: Computer Science University of Virginia Computer Science. Menu. History of Scheme LISP Lists Sum. History of Scheme. Scheme [1975] Guy Steele and Gerry Sussman Originally “Schemer” - PowerPoint PPT Presentation

Transcript of David Evans cs.virginia/~evans

Page 1: David Evans cs.virginia/~evans

David Evanshttp://www.cs.virginia.edu/~evans

CS200: Computer ScienceUniversity of VirginiaComputer Science

Lecture 8: Con’s car cdr sdr wdr

Page 2: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 2

Menu

• History of Scheme–LISP

• Lists

• Sum

Page 3: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 3

History of Scheme• Scheme [1975]

– Guy Steele and Gerry Sussman

– Originally “Schemer”

– “Conniver” [1973] and “Planner” [1967]

• Based on LISP– John McCarthy (late 1950s)

• Based on Lambda Calculus– Alonzo Church (1930s)

– Last few lectures in course

Page 4: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 4

LISP

“Lots of Insipid Silly Parentheses”

“LISt Processing language”

Lists are pretty important – hard to write a useful Scheme program without them.

Page 5: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 5

Making Lists

Page 6: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 6

Making a Pair

> (cons 1 2)(1 . 2) 1 2

cons constructs a pair

Page 7: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 7

Splitting a Pair> (car (cons 1 2))1> (cdr (cons 1 2))2

1 2

cons

car cdr

car extracts first part of a paircdr extracts second part of a pair

Page 8: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 8

Why “car” and “cdr”?• Original (1950s) LISP on IBM 704

– Stored cons pairs in memory registers– car = “Contents of the Address part of the

Register”– cdr = “Contents of the Decrement part of the

Register” (“could-er”)

• Doesn’t matters unless you have an IBM 704

• Think of them as first and rest(define first car)(define rest cdr)

Page 9: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 9

Implementing cons, car and cdr

• Using PS2: (define cons make-point)(define car x-of-point)(define cdr y-of-point)

• As we implemented make-point, etc.:(define (cons a b) (lambda (w) (if (w) a

b)))(define (car pair) (pair #t)(define (cdr pair) (pair #f)

Page 10: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 10

Pairs are fine, but how do we make threesomes?

Page 11: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 11

Threesome?

(define (threesome a b c) (lambda (w) (if (= w 0) a (if (= w 1) b c))))

(define (first t) (t 0))(define (second t) (t 1))(define (third t) (t 2))Is there a better way of thinking about our triple?

Page 12: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 12

Triple

• A triple is just a pair where one of the parts is a pair!

(define (triple a b c) (cons a (cons b c)))(define (t-first t) (car t))(define (t-second t) (car (cdr t))) (define (t-third t) (cdr (cdr t)))

Page 13: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 13

Quadruple

• A quadruple is a pair where the second part is a triple

(define (quadruple a b c d) (cons a (triple b c d)))(define (q-first q) (car q))(define (q-second q) (t-first (cdr t))) (define (q-third t) (t-second (cdr t)))(define (q-fourth t) (t-third (cdr t)))

Page 14: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 14

Multuples• A quintuple is a pair where the second part is

a quadruple

• A sextuple is a pair where the second part is a quintuple

• A septuple is a pair where the second part is a sextuple

• An octuple is group of octupi

• A list (any length tuple) is a pair where the second part is a …?

Page 15: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 15

Lists

List ::= (cons Element List)

A list is a pair where the second part is a list.

One little problem: how do we stop?This only allows infinitely long lists!

Page 16: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 16

Recursive Transition Networks

ARTICLE ADJECTIVE NOUN endbegin

ORNATE NOUN

ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN

ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE NOUN

ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUNORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN

ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN

From Lecture 6

Page 17: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 17

Recursive Transition Networks

ARTICLE ADJECTIVE NOUN endbegin

ORNATE NOUN

ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN

ADJECTIVES ::= ADJECTIVE ADJECTIVES

ADJECTIVES ::=

Page 18: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 18

Lists

List ::= (cons Element List)List ::=

A list is either: a pair where the second part is a listor, empty

It’s hard to write this!

Page 19: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 19

Null

List ::= (cons Element List)List ::=

A list is either: a pair where the second part is a listor, empty (null)

null

Page 20: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 20

List Examples> null()> (cons 1 null)(1)> (list? null)#t> (list? (cons 1 2))#f> (list? (cons 1 null))#t

Page 21: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 21

More List Examples

> (list? (cons 1 (cons 2 null)))#t> (car (cons 1 (cons 2 null)))1> (cdr (cons 1 (cons 2 null)))(2)

Page 22: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 22

Sum

Page 23: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 23

Friday

(define (sum n) (for 1 n (lambda (acc i) (+ i acc)) 0))

(define (sum n) (for 1 n + 0))

Can we use lists to define sum?

(sum n) = 1 + 2 + … + n

Page 24: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 24

Sum

(define (sum n) (insertl + (intsto n)))

(intsto n) = (list 1 2 3 … n)(insertl + (list 1 2 3 … n)) = (+ 1 (+ 2 (+ 3 (+ … (+

n)))))

Page 25: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 25

Intsto (Dyzlexic implementation)

;;; Evaluates to the list (1 2 3 … n) if n >= 1,

;;; null if n = 0.

(define (intsto n)(if (= n 0) null (cons n (intsto (- n 1)))))

Page 26: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 26

Intsto?

;;; Evaluates to the list (1 2 3 … n) if n >= 1,

;;; null if n = 0.

(define (intsto n)(if (= n 0) null (list (intsto (- n 1)) n)))

> (intsto 5)(((((() 1) 2) 3) 4) 5)

Page 27: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 27

Intsto

;;; Evaluates to the list (1 2 3 … n) if n >= 1,

;;; null if n = 0.

(define (intsto n)(if (= n 0) null (append (intsto (- n 1)) (list n))))

> (intsto 5)(1 2 3 4 5)

append puts two lists together

Page 28: David Evans cs.virginia/~evans

4 February 2002 CS 200 Spring 2002 28

Charge

• PS3 Out Today– Uses Lists to make fractals

– Due next week Wednesday

• Next time:– We’ll define insertl

• Try to do this yourself! (Easier than defining for)