107
61A Lecture 24

61A Lecture 24 - inst.eecs.berkeley.edu

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 61A Lecture 24 - inst.eecs.berkeley.edu

61A Lecture 24

Page 2: 61A Lecture 24 - inst.eecs.berkeley.edu

Announcements

Page 3: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme

Page 4: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme is a Dialect of Lisp

4

Page 5: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme is a Dialect of Lisp

What are people saying about Lisp?

4

Page 6: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme is a Dialect of Lisp

What are people saying about Lisp?

• "If you don't know Lisp, you don't know what it means for a programming language to be powerful and elegant." - Richard Stallman, created Emacs & the first free variant of UNIX

4

Page 7: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme is a Dialect of Lisp

What are people saying about Lisp?

• "If you don't know Lisp, you don't know what it means for a programming language to be powerful and elegant." - Richard Stallman, created Emacs & the first free variant of UNIX

• "The only computer language that is beautiful." -Neal Stephenson, DeNero's favorite sci-fi author

4

Page 8: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme is a Dialect of Lisp

What are people saying about Lisp?

• "If you don't know Lisp, you don't know what it means for a programming language to be powerful and elegant." - Richard Stallman, created Emacs & the first free variant of UNIX

• "The only computer language that is beautiful." -Neal Stephenson, DeNero's favorite sci-fi author

• "The greatest single programming language ever designed." -Alan Kay, co-inventor of Smalltalk and OOP (from the user interface video)

4

Page 9: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Fundamentals

5

Page 10: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

5

Page 11: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

•Primitive expressions: 2 3.3 true + quotient

5

Page 12: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Fundamentals

Scheme programs consist of expressions, which can be:

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

5

Page 13: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 14: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 15: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 16: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 17: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 18: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 19: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 20: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 21: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 22: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 23: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 24: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 25: 61A Lecture 24 - inst.eecs.berkeley.edu

Special Forms

Page 26: 61A Lecture 24 - inst.eecs.berkeley.edu

Special Forms

7

Page 27: 61A Lecture 24 - inst.eecs.berkeley.edu

Special Forms

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

7

Page 28: 61A Lecture 24 - inst.eecs.berkeley.edu

Special Forms

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

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

7

Page 29: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 30: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 31: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 32: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 33: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 34: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 35: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 36: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 37: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 38: 61A Lecture 24 - inst.eecs.berkeley.edu

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)

Page 39: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Interpreters

(Demo)

Page 40: 61A Lecture 24 - inst.eecs.berkeley.edu

Lambda Expressions

Page 41: 61A Lecture 24 - inst.eecs.berkeley.edu

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

10

Page 42: 61A Lecture 24 - inst.eecs.berkeley.edu

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

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

10

Page 43: 61A Lecture 24 - inst.eecs.berkeley.edu

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

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

10

Page 44: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 45: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 46: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 47: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 48: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 49: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and Lists

Page 50: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and Lists

12

Page 51: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn the late 1950s, computer scientists used confusing names

12

Page 52: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn the late 1950s, computer scientists used confusing names• cons: Two-argument procedure that creates a pair

12

Page 53: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 54: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 55: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 56: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 57: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 58: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 59: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 60: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 61: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 62: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 63: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 64: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 65: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 66: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 67: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 68: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 69: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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

Page 70: 61A Lecture 24 - inst.eecs.berkeley.edu

Pairs and ListsIn 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)

Page 71: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

Page 72: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

14

Page 73: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

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

14

Page 74: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

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

> (define a 1)

14

Page 75: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

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

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

14

Page 76: 61A Lecture 24 - inst.eecs.berkeley.edu

Symbolic Programming

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

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

14

Page 77: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 78: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 79: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 80: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 81: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 82: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 83: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 84: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 85: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 86: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 87: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 88: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 89: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 90: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Lists and Quotation

15

Page 91: 61A Lecture 24 - inst.eecs.berkeley.edu

Scheme Lists and Quotation

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

15

Page 92: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 93: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 94: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 95: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 96: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 97: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 98: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 99: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 100: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 101: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 102: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 103: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 104: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 105: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 106: 61A Lecture 24 - inst.eecs.berkeley.edu

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

Page 107: 61A Lecture 24 - inst.eecs.berkeley.edu

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