61A Lecture 25 Friday, October 26 Scheme is a Dialect of Lisp 2 - - PowerPoint PPT Presentation

61a lecture 25
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 25 Friday, October 26 Scheme is a Dialect of Lisp 2 - - PowerPoint PPT Presentation

61A Lecture 25 Friday, October 26 Scheme is a Dialect of Lisp 2 Scheme is a Dialect of Lisp What are people saying about Lisp? 2 Scheme is a Dialect of Lisp What are people saying about Lisp? "The greatest single programming


slide-1
SLIDE 1

61A Lecture 25

Friday, October 26

slide-2
SLIDE 2

Scheme is a Dialect of Lisp

2

slide-3
SLIDE 3

Scheme is a Dialect of Lisp

What are people saying about Lisp?

2

slide-4
SLIDE 4

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "The greatest single programming language ever designed."
  • Alan Kay, co-inventor of Smalltalk and OOP

2

slide-5
SLIDE 5

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "The greatest single programming language ever designed."
  • Alan Kay, co-inventor of Smalltalk and OOP
  • "The only computer language that is beautiful."
  • Neal Stephenson, John's favorite sci-fi author

2

slide-6
SLIDE 6

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "The greatest single programming language ever designed."
  • Alan Kay, co-inventor of Smalltalk and OOP
  • "The only computer language that is beautiful."
  • Neal Stephenson, John's favorite sci-fi author
  • "God's programming language."
  • Brian Harvey, Berkeley CS instructor extraordinaire

2

slide-7
SLIDE 7

http://imgs.xkcd.com/comics/lisp_cycles.png

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "The greatest single programming language ever designed."
  • Alan Kay, co-inventor of Smalltalk and OOP
  • "The only computer language that is beautiful."
  • Neal Stephenson, John's favorite sci-fi author
  • "God's programming language."
  • Brian Harvey, Berkeley CS instructor extraordinaire

2

slide-8
SLIDE 8

Scheme Fundamentals

3

slide-9
SLIDE 9

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

3

slide-10
SLIDE 10

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...

3

slide-11
SLIDE 11

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

slide-12
SLIDE 12

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values.

slide-13
SLIDE 13

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands.

slide-14
SLIDE 14

> (quotient 10 2) 5

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands.

slide-15
SLIDE 15

> (quotient 10 2) 5

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-16
SLIDE 16

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-17
SLIDE 17

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-18
SLIDE 18

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-19
SLIDE 19

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-20
SLIDE 20

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-21
SLIDE 21

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-22
SLIDE 22

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-23
SLIDE 23

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2, 3.3, true, +, quotient, ...
  • Combinations: (quotient 10 2), (not true), ...

3

Numbers are self-evaluating; symbols are bound to values. Call expressions have an operator and 0 or more operands. Demo “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines (spacing doesn’t matter)

slide-24
SLIDE 24

Special Forms

4

slide-25
SLIDE 25

Special Forms

4

A combination that is not a call expression is a special form:

slide-26
SLIDE 26

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
slide-27
SLIDE 27

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
slide-28
SLIDE 28

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)
slide-29
SLIDE 29

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)

> (define pi 3.14) > (* pi 2) 6.28

slide-30
SLIDE 30

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)

> (define pi 3.14) > (* pi 2) 6.28 The name “pi” is bound to 3.14 in the global frame

slide-31
SLIDE 31

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)
  • New procedures: (define (<name> <formal parameters>) <body>)

> (define pi 3.14) > (* pi 2) 6.28 The name “pi” is bound to 3.14 in the global frame

slide-32
SLIDE 32

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)
  • New procedures: (define (<name> <formal parameters>) <body>)

> (define pi 3.14) > (* pi 2) 6.28 > (define (abs x) (if (< x 0) (- x) x)) > (abs -3) 3 The name “pi” is bound to 3.14 in the global frame

slide-33
SLIDE 33

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)
  • New procedures: (define (<name> <formal parameters>) <body>)

> (define pi 3.14) > (* pi 2) 6.28 > (define (abs x) (if (< x 0) (- x) x)) > (abs -3) 3 The name “pi” is bound to 3.14 in the global frame A procedure is created and bound to the name “abs”

slide-34
SLIDE 34

Special Forms

4

A combination that is not a call expression is a special form:

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding names: (define <name> <expression>)
  • New procedures: (define (<name> <formal parameters>) <body>)

> (define pi 3.14) > (* pi 2) 6.28 > (define (abs x) (if (< x 0) (- x) x)) > (abs -3) 3 The name “pi” is bound to 3.14 in the global frame A procedure is created and bound to the name “abs” Demo

slide-35
SLIDE 35

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

slide-36
SLIDE 36

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

(lambda (<formal-parameters>) <body>)

slide-37
SLIDE 37

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

λ

(lambda (<formal-parameters>) <body>)

slide-38
SLIDE 38

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

λ

(lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4)))

slide-39
SLIDE 39

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

λ

(lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too:

slide-40
SLIDE 40

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

λ

(lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3)

slide-41
SLIDE 41

Lambda Expressions

Lambda expressions evaluate to anonymous functions.

5

λ

(lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3) Evaluates to the add-x-&-y-&-z2 procedure

slide-42
SLIDE 42

Pairs and Lists

6

slide-43
SLIDE 43

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

slide-44
SLIDE 44

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
slide-45
SLIDE 45

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
slide-46
SLIDE 46

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
slide-47
SLIDE 47

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list
slide-48
SLIDE 48

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

slide-49
SLIDE 49

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

slide-50
SLIDE 50

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
slide-51
SLIDE 51

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists.

slide-52
SLIDE 52

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2)

slide-53
SLIDE 53

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2)

Not a well-formed list!

slide-54
SLIDE 54

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) > (car x) 1

Not a well-formed list!

slide-55
SLIDE 55

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2

Not a well-formed list!

slide-56
SLIDE 56

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4)

Not a well-formed list!

slide-57
SLIDE 57

Pairs and Lists

6

In the late 1950s, computer scientists used confusing names.

  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

They also used a non-obvious notation for recursive lists.

  • A (recursive) Scheme list is a pair in which the second element is

nil or a Scheme list.

  • Scheme lists are written as space-separated combinations.
  • A dotted list has an arbitrary value for the second element of the

last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4)

Not a well-formed list! Demo

slide-58
SLIDE 58

Symbolic Programming

7

slide-59
SLIDE 59

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

slide-60
SLIDE 60

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1)

slide-61
SLIDE 61

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2)

slide-62
SLIDE 62

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b)

slide-63
SLIDE 63

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2)

slide-64
SLIDE 64

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) No sign of “a” and “b” in the resulting value

slide-65
SLIDE 65

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value

slide-66
SLIDE 66

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b)

slide-67
SLIDE 67

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b)

slide-68
SLIDE 68

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b)

slide-69
SLIDE 69

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2)

slide-70
SLIDE 70

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Symbols are now values

slide-71
SLIDE 71

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. Symbols are now values

slide-72
SLIDE 72

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) Symbols are now values

slide-73
SLIDE 73

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a Symbols are now values

slide-74
SLIDE 74

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a > (cdr '(a b c)) Symbols are now values

slide-75
SLIDE 75

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols?

7

> (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a > (cdr '(a b c)) (b c) Symbols are now values

slide-76
SLIDE 76

Scheme Lists and Quotation

8

slide-77
SLIDE 77

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

slide-78
SLIDE 78

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3)))

slide-79
SLIDE 79

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3

slide-80
SLIDE 80

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists.

slide-81
SLIDE 81

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3)

slide-82
SLIDE 82

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3)

1 2 3

slide-83
SLIDE 83

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3)

1 2 3

slide-84
SLIDE 84

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4))

1 2 3

slide-85
SLIDE 85

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4))

1 2 3 1 2

slide-86
SLIDE 86

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4))

1 2 3 1 2 3 4 nil

slide-87
SLIDE 87

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4)

1 2 3 1 2 3 4 nil

slide-88
SLIDE 88

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil)

1 2 3 1 2 3 4 nil

slide-89
SLIDE 89

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil)

1 2 3 1 2 3 4 nil 1 2 3 nil

slide-90
SLIDE 90

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil) (1 2 3)

1 2 3 1 2 3 4 nil 1 2 3 nil

slide-91
SLIDE 91

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil) (1 2 3) What is the printed result of evaluating this expression?

1 2 3 1 2 3 4 nil 1 2 3 nil

slide-92
SLIDE 92

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil) (1 2 3) What is the printed result of evaluating this expression? > (cdr '((1 2) . (3 4 . (5))))

1 2 3 1 2 3 4 nil 1 2 3 nil

slide-93
SLIDE 93

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair.

8

> (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) (1 2 . 3) > '(1 2 . (3 4)) (1 2 3 4) > '(1 2 3 . nil) (1 2 3) What is the printed result of evaluating this expression? > (cdr '((1 2) . (3 4 . (5)))) (3 4 5)

1 2 3 1 2 3 4 nil 1 2 3 nil

slide-94
SLIDE 94

Coercing a Sorted List to a Binary Search Tree

9

slide-95
SLIDE 95

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 5 6 7

slide-96
SLIDE 96

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7

slide-97
SLIDE 97

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part

slide-98
SLIDE 98

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part

slide-99
SLIDE 99

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part

slide-100
SLIDE 100

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part

slide-101
SLIDE 101

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry

slide-102
SLIDE 102

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry

slide-103
SLIDE 103

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry

slide-104
SLIDE 104

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry Recursively coerce the right part

slide-105
SLIDE 105

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry Recursively coerce the right part

slide-106
SLIDE 106

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry Recursively coerce the right part

slide-107
SLIDE 107

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry Recursively coerce the right part

slide-108
SLIDE 108

Coercing a Sorted List to a Binary Search Tree

9

1 2 3 4 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 5 6 7 Recursively coerce the left part The next element is the entry Recursively coerce the right part

slide-109
SLIDE 109

The Let Special Form

10

slide-110
SLIDE 110

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...)

slide-111
SLIDE 111

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...)

1 2 3 4 5 6 7

slide-112
SLIDE 112

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements))))

1 2 3 4 5 6 7

slide-113
SLIDE 113

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n)

1 2 3 4 5 6 7

slide-114
SLIDE 114

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts)

1 2 3 4 5 6 7

slide-115
SLIDE 115

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size)))

1 2 3 4 5 6 7

slide-116
SLIDE 116

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size)))

1 2 3 4 5 6 7

slide-117
SLIDE 117

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1))))

1 2 3 4 5 6 7

slide-118
SLIDE 118

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1))))

1 2 3 4 5 6 7

slide-119
SLIDE 119

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts))

1 2 3 4 5 6 7

slide-120
SLIDE 120

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts))

1 2 3 4 5 6 7

slide-121
SLIDE 121

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size)))

1 2 3 4 5 6 7

slide-122
SLIDE 122

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size)))

1 2 3 4 5 6 7

slide-123
SLIDE 123

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size))) (let ((right-tree (car right-result)) (remaining-elts (cdr right-result)))

1 2 3 4 5 6 7

slide-124
SLIDE 124

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size))) (let ((right-tree (car right-result)) (remaining-elts (cdr right-result)))

1 2 3 4 5 6 7

slide-125
SLIDE 125

The Let Special Form

10

(define (entry tree) ...) (define (left-branch tree) ...) (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size))) (let ((right-tree (car right-result)) (remaining-elts (cdr right-result))) (cons (make-tree this-entry left-tree right-tree) remaining-elts))))))))

1 2 3 4 5 6 7

slide-126
SLIDE 126

The Begin Special Form

11

(begin <exp1> <exp2> ... <expn>)

slide-127
SLIDE 127

The Begin Special Form

11

Demo (begin <exp1> <exp2> ... <expn>)