lists, higher order procedures, and symbols - 6.037 ... · lists, higher order procedures, and...

349
Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Lecture 2 Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 1 / 65

Upload: others

Post on 22-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists, higher order procedures, and symbols6.037 - Structure and Interpretation of Computer Programs

Mike Phillips (mpp)

Massachusetts Institute of Technology

Lecture 2

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 1 / 65

Page 2: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Administrivia

Project 0 was due todayReminder: Project 1 due at 7pm on TuesdayMail to [email protected]

If you didn’t sign up on Tuesday, let us know

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 2 / 65

Page 3: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) =>

15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

Addition is not defined for stringsOnly works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 4: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

Addition is not defined for stringsOnly works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 5: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) => 15

(+ "hi" 15) =>

+: expects type <number> as 1st argument,given: "hi"; other arguments were: 15

Addition is not defined for stringsOnly works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 6: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

Addition is not defined for stringsOnly works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 7: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

Addition is not defined for strings

Only works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 8: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Types

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

Addition is not defined for stringsOnly works for things of type numberScheme checks types for simple built-in functions

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 3 / 65

Page 9: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Simple data types

Everything has a type:Number

StringBooleanProcedures?

Is the type of not the same type as + ?

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 4 / 65

Page 10: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Simple data types

Everything has a type:NumberString

BooleanProcedures?

Is the type of not the same type as + ?

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 4 / 65

Page 11: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Simple data types

Everything has a type:NumberStringBoolean

Procedures?Is the type of not the same type as + ?

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 4 / 65

Page 12: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Simple data types

Everything has a type:NumberStringBooleanProcedures?

Is the type of not the same type as + ?

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 4 / 65

Page 13: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Simple data types

Everything has a type:NumberStringBooleanProcedures?

Is the type of not the same type as + ?

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 4 / 65

Page 14: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

What about procedures?

Procedures have their own types, based on arguments and returnvaluenumber 7→ number means “takes one number, returns a number”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 5 / 65

Page 15: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

What is the type of +?

number, number 7→ number(mostly)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 6 / 65

Page 16: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

What is the type of +?number, number 7→ number

(mostly)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 6 / 65

Page 17: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

(+ 5 10) => 15

(+ "hi" 15) =>+: expects type <number> as 1st argument,

given: "hi"; other arguments were: 15

What is the type of +?number, number 7→ number(mostly)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 6 / 65

Page 18: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:

numberstringnumber 7→ numbernumber, number 7→ boolean

Type of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 19: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:number

stringnumber 7→ numbernumber, number 7→ boolean

Type of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 20: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:numberstring

number 7→ numbernumber, number 7→ boolean

Type of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 21: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:numberstringnumber 7→ number

number, number 7→ booleanType of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 22: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:numberstringnumber 7→ numbernumber, number 7→ boolean

Type of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 23: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Type examples

Expression:15"hi"square>

. . . is of type:numberstringnumber 7→ numbernumber, number 7→ boolean

Type of a procedure is a contractIf the operands have the specified types, the procedure will resultin a value of the specified typeOtherwise, its behavior is undefined

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 7 / 65

Page 24: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number

,

number

,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 25: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number

,

number

,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 26: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number

,

number

,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 27: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number,

number

,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 28: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number,

number

,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 29: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number,

number

7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 30: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 31: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 32: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→

number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 33: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 34: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 35: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean

7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 36: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean 7→

string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 37: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean 7→ string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 38: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean 7→ string

(lambda (x)(* 3.14 (* 2 5)))

any

7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 39: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean 7→ string

(lambda (x)(* 3.14 (* 2 5)))

any 7→

number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 40: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

More complicated examples

(lambda (a b c)(if (> a 0) (+ b c) (- b c)))

number, number, number 7→ number

(lambda (p)(if p "hi" "bye"))

boolean 7→ string

(lambda (x)(* 3.14 (* 2 5)))

any 7→ number

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 8 / 65

Page 41: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Patterns across procedures

Procedural abstraction is finding patterns, and making procedures ofthem:

(* 17 17)

(* 42 42)

(* x x)

. . .

(lambda (x) (* x x))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 9 / 65

Page 42: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Patterns across procedures

Procedural abstraction is finding patterns, and making procedures ofthem:

(* 17 17)

(* 42 42)

(* x x)

. . .(lambda (x) (* x x))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 9 / 65

Page 43: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

1 + 2 + . . .+ 1001 + 4 + 9 + . . .+ 1002

1 + 132 + 1

52 + . . .+ 1992 ≈ π2

8

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 10 / 65

Page 44: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))

(define (sum-squares a b)(if (> a b) 0

(+ (square a) (sum-squares (+ 1 a) b))))(define (pi-sum a b)

(if (> a b) 0(+ (/ 1 (square a))

(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 45: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))(define (sum-squares a b)

(if (> a b) 0(+ (square a) (sum-squares (+ 1 a) b))))

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 46: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))(define (sum-squares a b)

(if (> a b) 0(+ (square a) (sum-squares (+ 1 a) b))))

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 47: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))(define (sum-squares a b)

(if (> a b) 0(+ (square a) (sum-squares (+ 1 a) b))))

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 48: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))(define (sum-squares a b)

(if (> a b) 0(+ (square a) (sum-squares (+ 1 a) b))))

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 49: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summation

(define (sum-integers a b)(if (> a b) 0

(+ a (sum-integers (+ 1 a) b))))(define (sum-squares a b)

(if (> a b) 0(+ (square a) (sum-squares (+ 1 a) b))))

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 11 / 65

Page 50: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

) , number , (

number

7→

number

) , number

7→

number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 51: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

) , number , (

number

7→

number

) , number

7→

number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 52: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

) , number , (

number

7→

number

) , number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 53: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

) , number , (

number

7→

number

) , number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 54: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

)

,

number

,

(

number

7→

number

)

,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 55: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

)

,

number

,

(

number

7→

number

)

,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 56: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(

number

7→

number

) ,

number

,

(

number

7→

number

)

,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 57: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) ,

number

,

(

number

7→

number

)

,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 58: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) , number ,

(

number

7→

number

)

,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 59: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) , number , (

number

7→

number

) ,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 60: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) , number , (number7→number) ,

number

7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 61: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) , number , (number7→number) , number 7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 62: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Complex types

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

What is the type of this procedure?

(number7→number) , number , (number7→number) , number 7→ number

What type is the output?

How many arguments does it have?

What is the type of each argument?

Higher-order procedures take a procedure as an argument, or return one as avalue

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 12 / 65

Page 63: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum

(lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 64: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum

(lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 65: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum

(lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 66: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum

(lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 67: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum (lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 68: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum (lambda (x) x)

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 69: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k

(define (sum-integers a b)(if (> a b) 0

(+ a(sum-integers (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-integers a b)(sum (lambda (x) x)

a(lambda (x) (+ x 1))b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 13 / 65

Page 70: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum

square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 71: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum

square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 72: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum

square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 73: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum

square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 74: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 75: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum square

a

(lambda (x) (+ x 1))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 76: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k2

(define (sum-squares a b)(if (> a b) 0

(+ (square a)(sum-squares (+ 1 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-sum-squares a b)(sum square

a(lambda (x) (+ x 1))b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 14 / 65

Page 77: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum

(lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 78: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum

(lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 79: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum

(lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 80: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum

(lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 81: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 82: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x)))

a

(lambda (x) (+ x 2))

b))Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 83: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Higher-order procedures

b∑k=a

k odd

1k2 ≈

π2

8

(define (pi-sum a b)(if (> a b) 0

(+ (/ 1 (square a))(pi-sum (+ 2 a) b))))

(define (sum term a next b)(if (> a b) 0

(+ (term a)(sum term (next a) next b))))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x)))

a(lambda (x) (+ x 2))b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 15 / 65

Page 84: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 85: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 86: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 87: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))

(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 88: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 89: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))

(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 90: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))

(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 91: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 92: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

. . . takes a procedure as an argument or returns one as a value

(define (new-sum-integers a b)(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

(define (new-sum-squares a b)(sum square a (lambda (x) (+ x 1)) b))

(define (add1 x) (+ x 1))(define (new-sum-squares a b) (sum square a add1 b))

(define (new-pi-sum a b)(sum (lambda (x) (/ 1 (square x))) a

(lambda (x) (+ x 2)) b))(define (add2 x) (+ x 2))(define (new-pi-sum a b)

(sum (lambda (x) (/ 1 (square x))) a add2 b))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 16 / 65

Page 93: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 94: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 95: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))

(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 96: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))

(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 97: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 98: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:

number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 99: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define (add1 x) (+ x 1))(define (add2 x) (+ x 2))

(define incrementby (lambda (n) ... ))

(define add1 (incrementby 1))(define add2 (incrementby 2))(define add37.5 (incrementby 37.5))

type of incrementby:number 7→ (number 7→ number)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 17 / 65

Page 100: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n)

(lambda (x) (+ x n))

))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 101: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 102: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )

( (lambda (n) (lambda (x) (+ x n))) 2 )(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 103: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 104: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 105: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)

((lambda (x) (+ x 2)) 4)(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 106: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 107: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)

6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 108: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Returning procedures

(define incrementby; type: num -> (num->num)(lambda (n) (lambda (x) (+ x n))))

( incrementby 2 )( (lambda (n) (lambda (x) (+ x n))) 2 )

(lambda (x) (+ x 2))

( (incrementby 2) 4)((lambda (x) (+ x 2)) 4)

(+ 4 2)6

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 18 / 65

Page 109: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x) (try 1 x))

(define try (lambda (guess x)(if (good-enough? guess x)

guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 19 / 65

Page 110: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x) (try 1 x))(define try (lambda (guess x)

(if (good-enough? guess x)guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 19 / 65

Page 111: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x) (try 1 x))(define try (lambda (guess x)

(if (good-enough? guess x)guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 19 / 65

Page 112: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x) (try 1 x))(define try (lambda (guess x)

(if (good-enough? guess x)guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 19 / 65

Page 113: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x) (try 1 x))(define try (lambda (guess x)

(if (good-enough? guess x)guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 19 / 65

Page 114: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x)(define try (lambda (guess x)

(if (good-enough? guess x)guess(try (improve guess x) x))))

(define good-enough? (lambda (guess x)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess x)(average guess (/ x guess))))

(try 1 x))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 20 / 65

Page 115: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Procedural abstraction

(define sqrt (lambda (x)(define try (lambda (guess

x

)(if (good-enough? guess

x

)guess(try (improve guess

x

)

x

))))(define good-enough? (lambda (guess

x

)(< (abs (- (square guess)

x))0.001)))

(define improve (lambda (guess

x

)(average guess (/ x guess))))

(try 1

x

))

(define average (lambda (a b)(/ (+ a b) 2)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 20 / 65

Page 116: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of values

Every value has a typeProcedure types (types which include 7→) indicate:

Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprogramsUseful for preventing some common types of errorsBasis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 117: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of valuesEvery value has a type

Procedure types (types which include 7→) indicate:Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprogramsUseful for preventing some common types of errorsBasis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 118: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of valuesEvery value has a typeProcedure types (types which include 7→) indicate:

Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprogramsUseful for preventing some common types of errorsBasis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 119: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of valuesEvery value has a typeProcedure types (types which include 7→) indicate:

Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprograms

Useful for preventing some common types of errorsBasis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 120: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of valuesEvery value has a typeProcedure types (types which include 7→) indicate:

Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprogramsUseful for preventing some common types of errors

Basis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 121: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Summary of types

A type is a set of valuesEvery value has a typeProcedure types (types which include 7→) indicate:

Number of arguments requiredType of each argumentType of the return value

They provide a mathematical theory for reasoning efficiently aboutprogramsUseful for preventing some common types of errorsBasis for many analysis and optimization algorithms

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 21 / 65

Page 122: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Compound data

Need a way of (procedure for) gluing data elements together into aunit that can be treated as a simple data element

Need ways of (procedures for) getting the pieces back outNeed a contract between “glue” and “unglue”Ideally want this “gluing” to have the property of closure:“The result obtained by creating a compound data structure canitself be treated as a primitive object and thus be input to thecreation of another compound object.”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 22 / 65

Page 123: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Compound data

Need a way of (procedure for) gluing data elements together into aunit that can be treated as a simple data elementNeed ways of (procedures for) getting the pieces back out

Need a contract between “glue” and “unglue”Ideally want this “gluing” to have the property of closure:“The result obtained by creating a compound data structure canitself be treated as a primitive object and thus be input to thecreation of another compound object.”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 22 / 65

Page 124: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Compound data

Need a way of (procedure for) gluing data elements together into aunit that can be treated as a simple data elementNeed ways of (procedures for) getting the pieces back outNeed a contract between “glue” and “unglue”

Ideally want this “gluing” to have the property of closure:“The result obtained by creating a compound data structure canitself be treated as a primitive object and thus be input to thecreation of another compound object.”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 22 / 65

Page 125: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Compound data

Need a way of (procedure for) gluing data elements together into aunit that can be treated as a simple data elementNeed ways of (procedures for) getting the pieces back outNeed a contract between “glue” and “unglue”Ideally want this “gluing” to have the property of closure:

“The result obtained by creating a compound data structure canitself be treated as a primitive object and thus be input to thecreation of another compound object.”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 22 / 65

Page 126: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Compound data

Need a way of (procedure for) gluing data elements together into aunit that can be treated as a simple data elementNeed ways of (procedures for) getting the pieces back outNeed a contract between “glue” and “unglue”Ideally want this “gluing” to have the property of closure:“The result obtained by creating a compound data structure canitself be treated as a primitive object and thus be input to thecreation of another compound object.”

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 22 / 65

Page 127: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs (cons cells)

(cons <a> <b>)→ <p>

Where <a> and <b> are expressions that map to <a-val> and<b-val>

Returns a pair <p> whose car-part is <a-val> and whosecdr-part is <b-val>(car <p>)→ <a-val>

(cdr <p>)→ <b-val>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 23 / 65

Page 128: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs (cons cells)

(cons <a> <b>)→ <p>

Where <a> and <b> are expressions that map to <a-val> and<b-val>

Returns a pair <p> whose car-part is <a-val> and whosecdr-part is <b-val>(car <p>)→ <a-val>

(cdr <p>)→ <b-val>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 23 / 65

Page 129: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs (cons cells)

(cons <a> <b>)→ <p>

Where <a> and <b> are expressions that map to <a-val> and<b-val>

Returns a pair <p> whose car-part is <a-val> and whosecdr-part is <b-val>

(car <p>)→ <a-val>

(cdr <p>)→ <b-val>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 23 / 65

Page 130: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs (cons cells)

(cons <a> <b>)→ <p>

Where <a> and <b> are expressions that map to <a-val> and<b-val>

Returns a pair <p> whose car-part is <a-val> and whosecdr-part is <b-val>(car <p>)→ <a-val>

(cdr <p>)→ <b-val>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 23 / 65

Page 131: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are tasty

(define p1 (cons 4 (+ 3 2)))

(car p1) ; -> 4

(cdr p1) ; -> 5

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 24 / 65

Page 132: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are tasty

(define p1 (cons 4 (+ 3 2)))

(car p1) ; ->

4

(cdr p1) ; -> 5

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 24 / 65

Page 133: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are tasty

(define p1 (cons 4 (+ 3 2)))

(car p1) ; -> 4

(cdr p1) ; -> 5

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 24 / 65

Page 134: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are tasty

(define p1 (cons 4 (+ 3 2)))

(car p1) ; -> 4

(cdr p1) ; ->

5

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 24 / 65

Page 135: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are tasty

(define p1 (cons 4 (+ 3 2)))

(car p1) ; -> 4

(cdr p1) ; -> 5

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 24 / 65

Page 136: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwiseAbstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 25 / 65

Page 137: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwiseAbstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 25 / 65

Page 138: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwiseAbstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 25 / 65

Page 139: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwise

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 25 / 65

Page 140: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pair abstraction

Once we build a pair, we can treat it as if it were a primitivePairs have the property of closure — we can use a pair anywherewe would expect to use a primitive data element:(cons (cons 1 2) 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 26 / 65

Page 141: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

What type is make-point?

number, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 27 / 65

Page 142: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))

(define p2 (make-point 4 1))

What type is make-point?

number, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 27 / 65

Page 143: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

What type is make-point?

number, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 27 / 65

Page 144: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

What type is make-point?

number, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 27 / 65

Page 145: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

What type is make-point?

number, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 27 / 65

Page 146: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building data abstractions

(define make-point cons)(define point-x car)(define point-y cdr)

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 28 / 65

Page 147: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building on earlier abstraction

;;; Point abstraction(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))

(define p1 (make-point 2 3))(define p2 (make-point 4 1))

;;; Segment abstraction(define (make-seg pt1 pt2)

(cons pt1 pt2))(define (start-point seg)

(car seg))(define (end-point seg)

(cdr seg))(define s1 (make-seg p1 p2))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 29 / 65

Page 148: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building on earlier abstraction

;;; Point abstraction(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))(define p1 (make-point 2 3))(define p2 (make-point 4 1))

;;; Segment abstraction(define (make-seg pt1 pt2)

(cons pt1 pt2))(define (start-point seg)

(car seg))(define (end-point seg)

(cdr seg))(define s1 (make-seg p1 p2))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 29 / 65

Page 149: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building on earlier abstraction

;;; Point abstraction(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))(define p1 (make-point 2 3))(define p2 (make-point 4 1))

;;; Segment abstraction(define (make-seg pt1 pt2)(cons pt1 pt2))

(define (start-point seg)(car seg))

(define (end-point seg)(cdr seg))

(define s1 (make-seg p1 p2))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 29 / 65

Page 150: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Building on earlier abstraction

;;; Point abstraction(define (make-point x y) (cons x y))(define (point-x point) (car point))(define (point-y point) (cdr point))(define p1 (make-point 2 3))(define p2 (make-point 4 1))

;;; Segment abstraction(define (make-seg pt1 pt2)(cons pt1 pt2))

(define (start-point seg)(car seg))

(define (end-point seg)(cdr seg))

(define s1 (make-seg p1 p2))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 29 / 65

Page 151: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 152: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 153: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 154: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) ->

(4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 155: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)

p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 156: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 ->

(2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 157: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 30 / 65

Page 158: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

What type is stretch-point?

Point, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 31 / 65

Page 159: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

What type is stretch-point?

Point, number 7→ Point

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 31 / 65

Page 160: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-seg seg scale)(make-seg (stretch-point (start-point seg) scale)

(stretch-point (end-point seg) scale)))(define (seg-length seg)

(sqrt (+ (square(- (point-x (start-point seg))

(point-x (end-point seg))))(square(- (point-y (start-point seg))

(point-y (end-point seg)))))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 32 / 65

Page 161: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-seg seg scale)(make-seg (stretch-point (start-point seg) scale)

(stretch-point (end-point seg) scale)))(define (seg-length seg)(sqrt (+ (square

(- (point-x (start-point seg))(point-x (end-point seg))))

(square(- (point-y (start-point seg))

(point-y (end-point seg)))))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 32 / 65

Page 162: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-seg seg scale)(make-seg (stretch-point (start-point seg) scale)

(stretch-point (end-point seg) scale)))(define (seg-length seg)(sqrt (+ (square

(- (point-x (start-point seg))(point-x (end-point seg))))

(square(- (point-y (start-point seg))

(point-y (end-point seg)))))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 32 / 65

Page 163: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-seg seg scale)(make-seg (stretch-point (start-point seg) scale)

(stretch-point (end-point seg) scale)))(define (seg-length seg)(sqrt (+ (square

(- (point-x (start-point seg))(point-x (end-point seg))))

(square(- (point-y (start-point seg))

(point-y (end-point seg)))))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 32 / 65

Page 164: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(make-point (* scale (point-x pt))

(* scale (point-y pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 33 / 65

Page 165: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using data abstractions

(define p1 (make-point 2 3))(define p2 (make-point 4 1))(define s1 (make-seg p1 p2))

(define (stretch-point pt scale)(cons (* scale (car pt))

(* scale (cdr pt))))

(stretch-point p1 2) -> (4 . 6)p1 -> (2 . 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 33 / 65

Page 166: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Abstractions have two communities

Builders(define (make-point x y) (cons x y))(define (point-x point) (car point))

Users(* scale (point-x pt))

Frequently the same person

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 34 / 65

Page 167: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Abstractions have two communities

Builders(define (make-point x y) (cons x y))(define (point-x point) (car point))

Users(* scale (point-x pt))

Frequently the same person

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 34 / 65

Page 168: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwise

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 35 / 65

Page 169: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Pairs are a data abstraction

Constructor(cons A B) 7→ Pair<A,B>

Accessors(car Pair<A,B>) 7→ A(cdr Pair<A,B>) 7→ B

Contract(car (cons A B)) 7→ A(cdr (cons A B)) 7→ B

Operations(pair? Q) returns #t if Q evaluates to a pair, #f otherwiseAbstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 35 / 65

Page 170: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

A rational number is a ratio nd

Addition:ab+

cd

=ad + bc

bd

23+

14=

2 · 4 + 3 · 112

=1112

Multiplication:ab· c

d=

acbd

23· 1

3=

29

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 36 / 65

Page 171: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

A rational number is a ratio nd

Addition:ab+

cd

=ad + bc

bd

23+

14=

2 · 4 + 3 · 112

=1112

Multiplication:ab· c

d=

acbd

23· 1

3=

29

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 36 / 65

Page 172: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

A rational number is a ratio nd

Addition:ab+

cd

=ad + bc

bd

23+

14=

2 · 4 + 3 · 112

=1112

Multiplication:ab· c

d=

acbd

23· 1

3=

29

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 36 / 65

Page 173: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

A rational number is a ratio nd

Addition:ab+

cd

=ad + bc

bd

23+

14=

2 · 4 + 3 · 112

=1112

Multiplication:ab· c

d=

acbd

23· 1

3=

29

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 36 / 65

Page 174: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

A rational number is a ratio nd

Addition:ab+

cd

=ad + bc

bd

23+

14=

2 · 4 + 3 · 112

=1112

Multiplication:ab· c

d=

acbd

23· 1

3=

29

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 36 / 65

Page 175: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

Constructor; make-rat: integer, integer -> Rat(make-rat <n> <d>) -> <r>

Accessors; numer, denom: Rat -> integer(numer <r>)(denom <r>)

Contract(numer (make-rat <n> <d>)) =⇒ <n>(denom (make-rat <n> <d>)) =⇒ <d>

Operations(+rat x y)(*rat x y)

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 37 / 65

Page 176: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

Constructor; make-rat: integer, integer -> Rat(make-rat <n> <d>) -> <r>

Accessors; numer, denom: Rat -> integer(numer <r>)(denom <r>)

Contract(numer (make-rat <n> <d>)) =⇒ <n>(denom (make-rat <n> <d>)) =⇒ <d>

Operations(+rat x y)(*rat x y)

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 37 / 65

Page 177: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

Constructor; make-rat: integer, integer -> Rat(make-rat <n> <d>) -> <r>

Accessors; numer, denom: Rat -> integer(numer <r>)(denom <r>)

Contract(numer (make-rat <n> <d>)) =⇒ <n>(denom (make-rat <n> <d>)) =⇒ <d>

Operations(+rat x y)(*rat x y)

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 37 / 65

Page 178: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

Constructor; make-rat: integer, integer -> Rat(make-rat <n> <d>) -> <r>

Accessors; numer, denom: Rat -> integer(numer <r>)(denom <r>)

Contract(numer (make-rat <n> <d>)) =⇒ <n>(denom (make-rat <n> <d>)) =⇒ <d>

Operations(+rat x y)(*rat x y)

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 37 / 65

Page 179: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

Constructor; make-rat: integer, integer -> Rat(make-rat <n> <d>) -> <r>

Accessors; numer, denom: Rat -> integer(numer <r>)(denom <r>)

Contract(numer (make-rat <n> <d>)) =⇒ <n>(denom (make-rat <n> <d>)) =⇒ <d>

Operations(+rat x y)(*rat x y)

Abstraction barrier

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 37 / 65

Page 180: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

ConstructorAccessorsContractOperationsAbstraction barrier

Implementation; Rat = Pair<integer, integer>(define (make-rat n d) (cons n d))(define (numer r) (car r))(define (denom r) (cdr r))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 38 / 65

Page 181: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rational number abstraction

ConstructorAccessorsContractOperationsAbstraction barrier

Implementation; Rat = Pair<integer, integer>(define (make-rat n d) (cons d n))(define (numer r) (cdr r))(define (denom r) (car r))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 38 / 65

Page 182: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Additional operators

; What is the type of +rat?

Rat, Rat -> Rat

(define (+rat x y)(make-rat (+ (* (numer x) (denom y))

(* (numer y) (denom x)))(* (denom x) (denom y))))

; The type of *rat:

Rat, Rat -> Rat

(define (*rat x y)(make-rat (* (numer x) (numer y))

(* (denom x) (denom y))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 39 / 65

Page 183: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Additional operators

; What is the type of +rat? Rat, Rat -> Rat(define (+rat x y)

(make-rat (+ (* (numer x) (denom y))(* (numer y) (denom x)))

(* (denom x) (denom y))))

; The type of *rat:

Rat, Rat -> Rat

(define (*rat x y)(make-rat (* (numer x) (numer y))

(* (denom x) (denom y))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 39 / 65

Page 184: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Additional operators

; What is the type of +rat? Rat, Rat -> Rat(define (+rat x y)

(make-rat (+ (* (numer x) (denom y))(* (numer y) (denom x)))

(* (denom x) (denom y))))

; The type of *rat:

Rat, Rat -> Rat

(define (*rat x y)(make-rat (* (numer x) (numer y))

(* (denom x) (denom y))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 39 / 65

Page 185: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Additional operators

; What is the type of +rat? Rat, Rat -> Rat(define (+rat x y)

(make-rat (+ (* (numer x) (denom y))(* (numer y) (denom x)))

(* (denom x) (denom y))))

; The type of *rat: Rat, Rat -> Rat(define (*rat x y)

(make-rat (* (numer x) (numer y))(* (denom x) (denom y))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 39 / 65

Page 186: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using our system

(define one-half (make-rat 1 2))(define three-fourths (make-rat 3 4))

(define new (+rat one-half three-fourths))

(numer new) ; ?(denom new) ; ?

We get 108 , not the simplified 5

4

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 40 / 65

Page 187: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Using our system

(define one-half (make-rat 1 2))(define three-fourths (make-rat 3 4))

(define new (+rat one-half three-fourths))

(numer new) ; 10(denom new) ; 8

We get 108 , not the simplified 5

4

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 40 / 65

Page 188: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rationalizing implementation

(define (gcd a b)(if (= b 0)

a(gcd b (remainder a b))))

(define (make-rat n d)(cons n d))

(define (numer r)(/ (car r) (gcd (car r) (cdr r))))

(define (denom r)(/ (cdr r) (gcd (car r) (cdr r))))

Remove common factors when accessed

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 41 / 65

Page 189: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rationalizing implementation

(define (gcd a b)(if (= b 0)

a(gcd b (remainder a b))))

(define (make-rat n d)(cons n d))

(define (numer r)(/ (car r) (gcd (car r) (cdr r))))

(define (denom r)(/ (cdr r) (gcd (car r) (cdr r))))

Remove common factors when accessed

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 41 / 65

Page 190: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rationalizing implementation

(define (gcd a b)(if (= b 0)

a(gcd b (remainder a b))))

(define (make-rat n d)(cons n d))

(define (numer r)(/ (car r) (gcd (car r) (cdr r))))

(define (denom r)(/ (cdr r) (gcd (car r) (cdr r))))

Remove common factors when accessed

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 41 / 65

Page 191: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Rationalizing implementation

(define (gcd a b)(if (= b 0)

a(gcd b (remainder a b))))

(define (make-rat n d)(cons (/ n (gcd n d))

(/ d (gcd n d))))(define (numer r)(car r))

(define (denom r)(cdr r))

Remove common factors when created

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 42 / 65

Page 192: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons r1 r2)(cons r3 r4))

(cons (cons r5 r6)(cons r7 r8)))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 193: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons r1 r2)

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 194: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons r1 r2)(cons r3 r4))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 195: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons r1 r2)(cons r3 r4))

r5)

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 196: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons r1 r2)(cons r3 r4))

(cons r5 r6))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 197: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons (cons r1 r2)(cons r3 r4))

(cons r5 r6))(cons r7 r8))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 198: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons r1 r2)(cons r3 r4))

(cons (cons r5 r6)(cons r7 r8)))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 199: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Grouping together larger collections

We want to group a set of rational numbers

(cons (cons (cons r1 r2)(cons r3 r4))

(cons (cons r5 r6)(cons r7 r8)))

. . .

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 43 / 65

Page 200: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Conventional interfaces — lists

A list is a type that can hold an arbitrary number of ordered items.

Formally, a list is a sequence of pairs with the following properties:

The car-part of a pair holds an itemThe cdr-part of a pair holds the rest of the listThe list is terminated by the empty list: ’()

Lists are closed under cons and cdr

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 44 / 65

Page 201: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Conventional interfaces — lists

A list is a type that can hold an arbitrary number of ordered items.Formally, a list is a sequence of pairs with the following properties:

The car-part of a pair holds an itemThe cdr-part of a pair holds the rest of the listThe list is terminated by the empty list: ’()

Lists are closed under cons and cdr

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 44 / 65

Page 202: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Conventional interfaces — lists

A list is a type that can hold an arbitrary number of ordered items.Formally, a list is a sequence of pairs with the following properties:

The car-part of a pair holds an itemThe cdr-part of a pair holds the rest of the listThe list is terminated by the empty list: ’()

Lists are closed under cons and cdr

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 44 / 65

Page 203: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 204: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 205: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 206: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 207: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; ->

(1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 208: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)

(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 209: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists and pairs as pictures

(cons <el1> <el2>)

el1

el2

(list <el1> <el2> ... <eln>)

el1 el2 eln

(list 1 2 3 4) ; -> (1 2 3 4)(null? <z>) ; -> #t if <z> evaluates to empty list

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 45 / 65

Page 210: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists

Sequences of cons cellsBetter, and safer, to abstract:(define first car)(define rest cdr)(define adjoin cons)

... but we don’t for lists and pairs

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 46 / 65

Page 211: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Lists

Sequences of cons cellsBetter, and safer, to abstract:(define first car)(define rest cdr)(define adjoin cons)

... but we don’t for lists and pairs

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 46 / 65

Page 212: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

cons’ing up lists

(define 1thru4 (list 1 2 3 4))

(define 2thru7 (list 2 3 4 5 6 7))

(define (enumerate from to)(if (> from to)

’()(cons from (enumerate (+ 1 from) to))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 47 / 65

Page 213: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

cons’ing up lists

(define 1thru4 (list 1 2 3 4))(define 2thru7 (list 2 3 4 5 6 7))

(define (enumerate from to)(if (> from to)

’()(cons from (enumerate (+ 1 from) to))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 47 / 65

Page 214: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

cons’ing up lists

(define 1thru4 (list 1 2 3 4))(define 2thru7 (list 2 3 4 5 6 7))

(define (enumerate from to)(if (> from to)

’()(cons from (enumerate (+ 1 from) to))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 47 / 65

Page 215: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

cdr’ing down lists

(define (length lst)(if (null? lst)

0(+ 1 (length (cdr lst)))))

(define (append list1 list2)(if (null? list1)

list2(cons (car list1)

(append (cdr list1)list2))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 48 / 65

Page 216: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

cdr’ing down lists

(define (length lst)(if (null? lst)

0(+ 1 (length (cdr lst)))))

(define (append list1 list2)(if (null? list1)

list2(cons (car list1)

(append (cdr list1)list2))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 48 / 65

Page 217: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Transforming lists

(define (square-list lst)(if (null? lst)

’()(cons (square (car lst))

(square-list (cdr lst)))))

(define (double-list lst)(if (null? lst)

’()(cons (* 2 (car lst))

(double-list (cdr lst)))))(define (map proc lst)

(if (null? lst)’()(cons (proc (car lst))

(map proc (cdr lst)))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 49 / 65

Page 218: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Transforming lists

(define (square-list lst)(if (null? lst)

’()(cons (square (car lst))

(square-list (cdr lst)))))(define (double-list lst)

(if (null? lst)’()(cons (* 2 (car lst))

(double-list (cdr lst)))))

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 49 / 65

Page 219: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Transforming lists

(define (square-list lst)(if (null? lst)

’()(cons (square (car lst))

(square-list (cdr lst)))))(define (double-list lst)

(if (null? lst)’()(cons (* 2 (car lst))

(double-list (cdr lst)))))

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 49 / 65

Page 220: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Transforming lists

(define (square-list lst)(if (null? lst)

’()(cons (square (car lst))

(square-list (cdr lst)))))(define (double-list lst)

(if (null? lst)’()(cons (* 2 (car lst))

(double-list (cdr lst)))))(define (map proc lst)

(if (null? lst)’()(cons (proc (car lst))

(map proc (cdr lst)))))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 49 / 65

Page 221: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?

(

A

7→

B

)

,

List<

A

>

7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 222: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?

(

A

7→

B

)

,

List<

A

>

7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 223: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(

A

7→

B

),

List<

A

>

7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 224: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(

A

7→

B

), List<

A

> 7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 225: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(

A

7→

B

), List<A> 7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 226: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(A 7→

B

), List<A> 7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 227: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(A 7→ B), List<A> 7→

List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 228: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(A 7→ B), List<A> 7→ List<

B

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 229: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Map

(define (map proc lst)(if (null? lst)

’()(cons (proc (car lst))

(map proc (cdr lst)))))

What is the type of map?(A 7→ B), List<A> 7→ List<B>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 50 / 65

Page 230: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6))

;-> (2 4 6)

What is the type of filter?

(

A

7→

Boolean

)

,

List<

A

>

7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 231: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?

(

A

7→

Boolean

)

,

List<

A

>

7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 232: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?

(

A

7→

Boolean

),

List<

A

>

7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 233: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(

A

7→

Boolean

), List<

A

> 7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 234: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(

A

7→

Boolean

), List<A> 7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 235: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(A 7→

Boolean

), List<A> 7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 236: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(A 7→ Boolean), List<A> 7→

List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 237: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(A 7→ Boolean), List<A> 7→ List<

A

>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 238: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Choosing just part of a list

(define (filter pred lst)(cond ((null? lst) ’())

((pred (car lst))(cons (car lst)

(filter pred (cdr lst))))(else (filter pred (cdr lst)))))

(filter even? (list 1 2 3 4 5 6));-> (2 4 6)

What is the type of filter?(A 7→ Boolean), List<A> 7→ List<A>

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 51 / 65

Page 239: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5

Characters and Strings: #\a "this is a string"Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 240: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"

Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 241: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"Booleans: #t, #f

Vectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 242: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 243: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)

Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 244: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)

Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 245: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Data Types in Scheme

ConventionalNumbers: 29, −35, 1.34, 1.2e5Characters and Strings: #\a "this is a string"Booleans: #t, #fVectors: #(1 2 3 "hi" 3.7)

Scheme-specificProcedures: value of +, result of evaluating (lambda (x) x)Pairs and lists: (42 . 8), (1 1 2 3 5 8 13)Symbols: pi, +, x, foo, hello-world

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 52 / 65

Page 246: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to proceduresReturn them as values of proceduresAssociate them as values of variablesStore them in data structures

For example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 247: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to proceduresReturn them as values of proceduresAssociate them as values of variablesStore them in data structures

For example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 248: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to procedures

Return them as values of proceduresAssociate them as values of variablesStore them in data structures

For example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 249: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to proceduresReturn them as values of procedures

Associate them as values of variablesStore them in data structures

For example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 250: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to proceduresReturn them as values of proceduresAssociate them as values of variables

Store them in data structuresFor example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 251: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Symbols

So far, we’ve seen them as the names of variables(define foo (+ bar 2))

But, in Scheme, all data types are first class, so we should be ableto:

Pass symbols as arguments to proceduresReturn them as values of proceduresAssociate them as values of variablesStore them in data structures

For example: (chocolate caffeine sugar)

chocolate caffeine sugar

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 53 / 65

Page 252: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.

We associate symbols with values using the special form define(define pi 3.1451926535)(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→ 3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 253: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.We associate symbols with values using the special form define

(define pi 3.1451926535)(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→ 3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 254: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.We associate symbols with values using the special form define(define pi 3.1451926535)

(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→ 3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 255: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.We associate symbols with values using the special form define(define pi 3.1451926535)(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→ 3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 256: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.We associate symbols with values using the special form define(define pi 3.1451926535)(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→

3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 257: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

How do we refer to Symbols?

Evaluation rule for symbolsValue of a symbol is the value it is associated with in theenvironment.We associate symbols with values using the special form define(define pi 3.1451926535)(* pi 2 r)

But how do we get to the symbol itself?(define baz pi) ??baz→ 3.1451926535

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 54 / 65

Page 258: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Referring to Symbols

Say your favorite color

Say “your favorite color”In the first case, we want the meaning associated with theexpressionIn the second, we want the expression itselfWe use the concept of quotation in Scheme to distinguishbetween these two cases

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 55 / 65

Page 259: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Referring to Symbols

Say your favorite colorSay “your favorite color”

In the first case, we want the meaning associated with theexpressionIn the second, we want the expression itselfWe use the concept of quotation in Scheme to distinguishbetween these two cases

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 55 / 65

Page 260: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Referring to Symbols

Say your favorite colorSay “your favorite color”In the first case, we want the meaning associated with theexpressionIn the second, we want the expression itself

We use the concept of quotation in Scheme to distinguishbetween these two cases

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 55 / 65

Page 261: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Referring to Symbols

Say your favorite colorSay “your favorite color”In the first case, we want the meaning associated with theexpressionIn the second, we want the expression itselfWe use the concept of quotation in Scheme to distinguishbetween these two cases

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 55 / 65

Page 262: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”

(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 263: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)

→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 264: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo

(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 265: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefined

baz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 266: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz

→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 267: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi

(+ pi baz)→ ERROR+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 268: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)

→ ERROR+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 269: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 270: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 271: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))

→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 272: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

New special form: quote

We want a way to tell the evaluator: “I want the following object aswhatever it is, not as an expression to be evaluated”(quote foo)→ foo(define baz (quote pi))→ undefinedbaz→ pi(+ pi baz)→ ERROR

+: expects type <number> as 2nd argument, given: pi; otherarguments were: 3.1415926535

(list (quote foo) (quote bar) (quote baz))→ (foo bar baz)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 56 / 65

Page 273: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cut

When it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 274: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluated

Examples:’pi→ pi’17→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 275: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi

→ pi’17→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 276: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi

’17→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 277: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17

→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 278: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17

’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 279: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17’"Hello world"

→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 280: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17’"Hello world"→ "Hello world"

’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 281: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17’"Hello world"→ "Hello world"’(1 2 3)

→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 282: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Syntactic sugar

The Reader (part of the Read-Eval-Print Loop, REPL) knows ashort-cutWhen it sees ’pi it acts just like it had read (quote pi)

The latter is what is actually evaluatedExamples:’pi→ pi’17→ 17’"Hello world"→ "Hello world"’(1 2 3)→ (1 2 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 57 / 65

Page 283: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar))

; -> (brains caffeine sugar)(list ’brains ’caffeine ’sugar)

; -> (brains caffeine sugar)’(brains caffeine sugar)

; -> (brains caffeine sugar)(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 284: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 285: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar)

; -> (brains caffeine sugar)’(brains caffeine sugar)

; -> (brains caffeine sugar)(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 286: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 287: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar)

; -> (brains caffeine sugar)(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 288: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 289: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))

(list (list ’foo ’bar) (list x y)(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 290: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee))

; -> ((foo bar) (42 (x y z))(baz quux squee))

’((foo bar) (x y) (bar quux squee)); -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 291: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))

’((foo bar) (x y) (bar quux squee)); -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 292: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 293: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Making list structures with symbols

(list (quote brains) (quote caffeine) (quote sugar)); -> (brains caffeine sugar)

(list ’brains ’caffeine ’sugar); -> (brains caffeine sugar)

’(brains caffeine sugar); -> (brains caffeine sugar)

(define x 42) (define y ’(x y z))(list (list ’foo ’bar) (list x y)

(list ’baz ’quux ’squee)); -> ((foo bar) (42 (x y z))

(baz quux squee))’((foo bar) (x y) (bar quux squee))

; -> ((foo bar) (x y) (bar quux squee))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 58 / 65

Page 294: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)

(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 295: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; ->

23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 296: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23

’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 297: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; ->

(+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 298: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)

(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 299: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; ->

(+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 300: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)

(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 301: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; ->

(+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 302: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)

(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 303: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; ->

(#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 304: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Confusing examples

(define x 20)(+ x 3) ; -> 23’(+ x 3) ; -> (+ x 3)(list (quote +) x ’3) ; -> (+ 20 3)(list ’+ x 3) ; -> (+ 20 3)(list + x 3) ; -> (#<procedure:+> 20 3)

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 59 / 65

Page 305: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols

(symbol? (quote foo))→ #t(symbol? ’foo)→ #t(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 306: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t

(symbol? ’foo)→ #t(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 307: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t(symbol? ’foo)→ #t

(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 308: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t(symbol? ’foo)→ #t(symbol? 4)→ #f

(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 309: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t(symbol? ’foo)→ #t(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f

(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 310: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t(symbol? ’foo)→ #t(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound to

eq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 311: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Operations on symbols

symbol? has type anytype → boolean, returns #t forsymbols(symbol? (quote foo))→ #t(symbol? ’foo)→ #t(symbol? 4)→ #f(symbol? ’(1 2 3))→ #f(symbol? foo)→ It depends on what value foo is bound toeq? tests the equality of symbols

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 60 / 65

Page 312: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

An aside: Testing for equality

eq? tests if two things are exactly the same object in memory. Notfor strings or numbers.

= tests the equality of numbersequal? tests if two things print the same– symbols, numbers,strings, lists of those, lists of lists

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 61 / 65

Page 313: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

An aside: Testing for equality

eq? tests if two things are exactly the same object in memory. Notfor strings or numbers.= tests the equality of numbers

equal? tests if two things print the same– symbols, numbers,strings, lists of those, lists of lists

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 61 / 65

Page 314: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

An aside: Testing for equality

eq? tests if two things are exactly the same object in memory. Notfor strings or numbers.= tests the equality of numbersequal? tests if two things print the same– symbols, numbers,strings, lists of those, lists of lists

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 61 / 65

Page 315: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; ->

#f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 316: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f

(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 317: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; ->

#t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 318: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t

(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 319: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; ->

#t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 320: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t

(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 321: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; ->

#f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 322: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f

(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 323: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; ->

#t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 324: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t

(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 325: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; ->

#f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 326: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 327: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; ->

Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 328: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!

(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 329: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; ->

#f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 330: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f

(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 331: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; ->

#t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 332: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 333: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; ->

#f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 334: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f

(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 335: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; ->

#t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 336: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t

(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 337: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))

(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 338: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))

(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 339: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; ->

#f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 340: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f

(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 341: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)

(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 342: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; ->

#t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 343: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

(= 4 10) ; -> #f(= 4 4) ; -> #t(equal? 4 4) ; -> #t(equal? (/ 1 2) 0.5) ; -> #f(eq? 4 4) ; -> #t(eq? (expt 2 70) (expt 2 70)) ; -> #f

(= "foo" "foo") ; -> Error!(eq? "foo" "foo") ; -> #f(equal? "foo" "foo") ; -> #t

(eq? ’(1 2) ’(1 2)) ; -> #f(equal? ’(1 2) ’(1 2)) ; -> #t(define a ’(1 2))(define b ’(1 2))(eq? a b) ; -> #f(define a b)(eq? a b) ; -> #t

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 62 / 65

Page 344: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Tagged data

Attaching a symbol to all data values that indicates the typeCan now determine if something is the type you expect

(define (make-point x y)(list x y))

(define (make-rat n d)(list x y))

(define (point? thing)(and (pair? thing)

(eq? (car thing) ’point)))

(define (rat? thing)(and (pair? thing)

(eq? (car thing) ’rat)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 63 / 65

Page 345: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Tagged data

Attaching a symbol to all data values that indicates the typeCan now determine if something is the type you expect

(define (make-point x y)(list ’point x y))

(define (make-rat n d)(list ’rat x y))

(define (point? thing)(and (pair? thing)

(eq? (car thing) ’point)))

(define (rat? thing)(and (pair? thing)

(eq? (car thing) ’rat)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 63 / 65

Page 346: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Tagged data

Attaching a symbol to all data values that indicates the typeCan now determine if something is the type you expect

(define (make-point x y)(list ’point x y))

(define (make-rat n d)(list ’rat x y))

(define (point? thing)(and (pair? thing)

(eq? (car thing) ’point)))

(define (rat? thing)(and (pair? thing)

(eq? (car thing) ’rat)))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 63 / 65

Page 347: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Benefits of tagged data

Data-directed programming - decide what to do based on type

(define (stretch thing scale)(if (point? thing)

(stretch-point thing scale)(stretch-seg thing scale)))

Defensive programming - Determine if something is the type youexpect, give a better error

(define (stretch-point pt)(if (not (point? pt))

(error "stretch-point passed a non-point:" pt);; ...carry on

))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 64 / 65

Page 348: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Benefits of tagged data

Data-directed programming - decide what to do based on type

(define (stretch thing scale)(if (point? thing)

(stretch-point thing scale)(stretch-seg thing scale)))

Defensive programming - Determine if something is the type youexpect, give a better error

(define (stretch-point pt)(if (not (point? pt))

(error "stretch-point passed a non-point:" pt);; ...carry on

))

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 64 / 65

Page 349: Lists, higher order procedures, and symbols - 6.037 ... · Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp)

Recitation time!

Mike Phillips (MIT) Lists, higher order procedures, and symbols Lecture 2 65 / 65