61A Lecture 24 Monday, March 30 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 24
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 24 Monday, March 30 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 24 Monday, March 30 Announcements 2 Announcements Homework 7 due Wednesday 4/8 @ 11:59pm 2 Announcements Homework 7 due Wednesday 4/8 @ 11:59pm Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm 2 Announcements


slide-1
SLIDE 1

61A Lecture 24

Monday, March 30

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm

2

slide-4
SLIDE 4

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm
  • Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm

2

slide-5
SLIDE 5

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm
  • Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm

§Open note, open interpreter, closed classmates, closed Internet

2

slide-6
SLIDE 6

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm
  • Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm

§Open note, open interpreter, closed classmates, closed Internet

  • Composition corrections for projects 1, 2, & 3 are due Monday 4/13 @ 11:59pm (do them now!)

2

slide-7
SLIDE 7

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm
  • Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm

§Open note, open interpreter, closed classmates, closed Internet

  • Composition corrections for projects 1, 2, & 3 are due Monday 4/13 @ 11:59pm (do them now!)

§Make changes to your project based on the composition feedback you received

2

slide-8
SLIDE 8

Announcements

  • Homework 7 due Wednesday 4/8 @ 11:59pm
  • Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm

§Open note, open interpreter, closed classmates, closed Internet

  • Composition corrections for projects 1, 2, & 3 are due Monday 4/13 @ 11:59pm (do them now!)

§Make changes to your project based on the composition feedback you received §Earn back any points you lost on composition

2

slide-9
SLIDE 9

Scheme

slide-10
SLIDE 10

Scheme is a Dialect of Lisp

4

slide-11
SLIDE 11

Scheme is a Dialect of Lisp

What are people saying about Lisp?

4

slide-12
SLIDE 12

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 (from the user interface video)

4

slide-13
SLIDE 13

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 (from the user interface video)
  • "The only computer language that is beautiful."

  • Neal Stephenson, DeNero's favorite sci-fi author

4

slide-14
SLIDE 14

Scheme Fundamentals

5

slide-15
SLIDE 15

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

5

slide-16
SLIDE 16

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

5

slide-17
SLIDE 17

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

5

slide-18
SLIDE 18

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values

5

slide-19
SLIDE 19

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

slide-20
SLIDE 20

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5

slide-21
SLIDE 21

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-22
SLIDE 22

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-23
SLIDE 23

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function)

slide-24
SLIDE 24

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-25
SLIDE 25

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-26
SLIDE 26

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-27
SLIDE 27

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-28
SLIDE 28

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-29
SLIDE 29

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values Call expressions include an operator and 0 or more operands in parentheses (Demo)

5

> (quotient 10 2) 5 > (quotient (+ 8 7) 5) 3 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) “quotient” names Scheme’s built-in integer division procedure (i.e., function) Combinations can span multiple lines 
 (spacing doesn’t matter)

slide-30
SLIDE 30

Special Forms

slide-31
SLIDE 31

Special Forms

7

slide-32
SLIDE 32

Special Forms

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

7

slide-33
SLIDE 33

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)

7

slide-34
SLIDE 34

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-35
SLIDE 35

Special Forms

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>)

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-36
SLIDE 36

Special Forms

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 symbols: (define <symbol> <expression>)

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-37
SLIDE 37

Special Forms

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 symbols: (define <symbol> <expression>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-38
SLIDE 38

Special Forms

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 symbols: (define <symbol> <expression>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-39
SLIDE 39

Special Forms

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 symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-40
SLIDE 40

Special Forms

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 symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-41
SLIDE 41

Special Forms

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 symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-42
SLIDE 42

Special Forms

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 symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative

slide-43
SLIDE 43

Special Forms

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 symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

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

7

Evaluation: (1) Evaluate the predicate expression (2) Evaluate either the consequent or alternative (Demo)

slide-44
SLIDE 44

Scheme Interpreters

(Demo)

slide-45
SLIDE 45

Lambda Expressions

slide-46
SLIDE 46

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

10

slide-47
SLIDE 47

Lambda Expressions

Lambda expressions evaluate to anonymous procedures (lambda (<formal-parameters>) <body>)

10

slide-48
SLIDE 48

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-49
SLIDE 49

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-50
SLIDE 50

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

(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:

10

slide-51
SLIDE 51

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

(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)

10

slide-52
SLIDE 52

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

(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 
 x+y+z2 procedure

10

slide-53
SLIDE 53

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

(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 
 x+y+z2 procedure

10

12

slide-54
SLIDE 54

Pairs and Lists

slide-55
SLIDE 55

Pairs and Lists

12

slide-56
SLIDE 56

Pairs and Lists

In the late 1950s, computer scientists used confusing names

12

slide-57
SLIDE 57

Pairs and Lists

In the late 1950s, computer scientists used confusing names

  • cons: Two-argument procedure that creates a pair

12

slide-58
SLIDE 58

Pairs and Lists

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

12

slide-59
SLIDE 59

Pairs and Lists

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

12

slide-60
SLIDE 60

Pairs and Lists

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

12

slide-61
SLIDE 61

Pairs and Lists

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 linked lists

12

slide-62
SLIDE 62

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.

12

slide-63
SLIDE 63

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces

12

slide-64
SLIDE 64

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

12

slide-65
SLIDE 65

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2))

12

slide-66
SLIDE 66

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x

12

slide-67
SLIDE 67

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2)

12

slide-68
SLIDE 68

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2) > (car x)

12

slide-69
SLIDE 69

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2) > (car x) 1

12

slide-70
SLIDE 70

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x)

12

slide-71
SLIDE 71

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2

12

slide-72
SLIDE 72

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil))))

12

slide-73
SLIDE 73

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (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)

12

slide-74
SLIDE 74

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (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!

12

slide-75
SLIDE 75

Pairs and Lists

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 linked lists

  • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list.
  • Important! Scheme lists are written in parentheses separated by spaces
  • A dotted list has any value for the second element of the last pair; maybe not a list!

> (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!

12

(Demo)

slide-76
SLIDE 76

Symbolic Programming

slide-77
SLIDE 77

Symbolic Programming

14

slide-78
SLIDE 78

Symbolic Programming

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

14

slide-79
SLIDE 79

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1)

14

slide-80
SLIDE 80

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2)

14

slide-81
SLIDE 81

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b)

14

slide-82
SLIDE 82

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2)

14

slide-83
SLIDE 83

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) No sign of “a” and “b” in the resulting value

14

slide-84
SLIDE 84

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-85
SLIDE 85

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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)

14

slide-86
SLIDE 86

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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)

14

slide-87
SLIDE 87

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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)

14

slide-88
SLIDE 88

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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)

14

slide-89
SLIDE 89

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-90
SLIDE 90

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-91
SLIDE 91

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-92
SLIDE 92

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-93
SLIDE 93

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-94
SLIDE 94

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (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

14

slide-95
SLIDE 95

Scheme Lists and Quotation

15

slide-96
SLIDE 96

Scheme Lists and Quotation

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

15

slide-97
SLIDE 97

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3)))

15

slide-98
SLIDE 98

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3

15

slide-99
SLIDE 99

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists.

15

slide-100
SLIDE 100

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3)

15

slide-101
SLIDE 101

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3)

1 2 3

15

slide-102
SLIDE 102

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-103
SLIDE 103

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-104
SLIDE 104

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-105
SLIDE 105

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-106
SLIDE 106

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-107
SLIDE 107

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-108
SLIDE 108

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-109
SLIDE 109

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-110
SLIDE 110

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-111
SLIDE 111

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-112
SLIDE 112

Scheme Lists and Quotation

Dots can be used in a quoted list to specify the second element of the final pair. > (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

15

slide-113
SLIDE 113

Sierpinski's Triangle

(Demo)