Scheme Announcements Scheme Scheme is a Dialect of Lisp 4 Scheme - - PowerPoint PPT Presentation

scheme announcements scheme scheme is a dialect of lisp
SMART_READER_LITE
LIVE PREVIEW

Scheme Announcements Scheme Scheme is a Dialect of Lisp 4 Scheme - - PowerPoint PPT Presentation

Scheme Announcements Scheme Scheme is a Dialect of Lisp 4 Scheme is a Dialect of Lisp What are people saying about Lisp? 4 Scheme is a Dialect of Lisp What are people saying about Lisp? "If you don't know Lisp, you don't know what


slide-1
SLIDE 1

Scheme

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Scheme

slide-4
SLIDE 4

Scheme is a Dialect of Lisp

4

slide-5
SLIDE 5

Scheme is a Dialect of Lisp

What are people saying about Lisp?

4

slide-6
SLIDE 6

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "If you don't know Lisp, you don't know what it means for a programming language to be

powerful and elegant."

  • Richard Stallman, created Emacs & the first free variant of UNIX

4

slide-7
SLIDE 7

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "If you don't know Lisp, you don't know what it means for a programming language to be

powerful and elegant."

  • Richard Stallman, created Emacs & the first free variant of UNIX
  • "The only computer language that is beautiful."
  • Neal Stephenson, DeNero's favorite sci-fi author

4

slide-8
SLIDE 8

Scheme is a Dialect of Lisp

What are people saying about Lisp?

  • "If you don't know Lisp, you don't know what it means for a programming language to be

powerful and elegant."

  • Richard Stallman, created Emacs & the first free variant of UNIX
  • "The only computer language that is beautiful."
  • Neal Stephenson, DeNero's favorite sci-fi author
  • "The greatest single programming language ever designed."
  • Alan Kay, co-inventor of Smalltalk and OOP (from the user interface video)

4

slide-9
SLIDE 9

Scheme Expressions

5

slide-10
SLIDE 10

Scheme Expressions

Scheme programs consist of expressions, which can be:

5

slide-11
SLIDE 11

Scheme Expressions

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2 3.3 true + quotient

5

slide-12
SLIDE 12

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

5

slide-13
SLIDE 13

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

Numbers are self-evaluating; symbols are bound to values

5

slide-14
SLIDE 14

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

slide-15
SLIDE 15

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

> (quotient 10 2) 5

slide-16
SLIDE 16

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-17
SLIDE 17

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-18
SLIDE 18

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-19
SLIDE 19

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-20
SLIDE 20

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-21
SLIDE 21

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-22
SLIDE 22

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-23
SLIDE 23

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-24
SLIDE 24

Scheme Expressions

Scheme programs consist of expressions, which can be:

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

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

5

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

slide-25
SLIDE 25

Special Forms

slide-26
SLIDE 26

Special Forms

7

slide-27
SLIDE 27

Special Forms

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

7

slide-28
SLIDE 28

Special Forms

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

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

7

slide-29
SLIDE 29

Special Forms

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

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

7

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

slide-30
SLIDE 30

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)
  • and and or: (and <e1> ... <en>), (or <e1> ... <en>)

7

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

slide-31
SLIDE 31

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)
  • and and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding symbols: (define <symbol> <expression>)

7

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

slide-32
SLIDE 32

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)
  • and and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding symbols: (define <symbol> <expression>)

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

7

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

slide-33
SLIDE 33

Special Forms

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

  • if expression: (if <predicate> <consequent> <alternative>)
  • and and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Binding symbols: (define <symbol> <expression>)

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

7

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

slide-34
SLIDE 34

Special Forms

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

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

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

7

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

slide-35
SLIDE 35

Special Forms

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

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

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

7

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

slide-36
SLIDE 36

Special Forms

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

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

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

7

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

slide-37
SLIDE 37

Special Forms

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

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

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

7

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

slide-38
SLIDE 38

Special Forms

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

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

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

7

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

slide-39
SLIDE 39

Scheme Interpreters

(Demo)

slide-40
SLIDE 40

Lambda Expressions

slide-41
SLIDE 41

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

10

slide-42
SLIDE 42

Lambda Expressions

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

10

slide-43
SLIDE 43

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-44
SLIDE 44

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-45
SLIDE 45

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-46
SLIDE 46

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-47
SLIDE 47

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

slide-48
SLIDE 48

Lambda Expressions

Lambda expressions evaluate to anonymous procedures

λ

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

10

12

slide-49
SLIDE 49

Sierpinski's Triangle

(Demo)

slide-50
SLIDE 50

More Special Forms

slide-51
SLIDE 51

Cond & Begin

13

slide-52
SLIDE 52

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

slide-53
SLIDE 53

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small')

slide-54
SLIDE 54

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small)))

slide-55
SLIDE 55

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small))

slide-56
SLIDE 56

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small)) (print )

slide-57
SLIDE 57

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small)) (print ) The begin special form combines multiple expressions into one expression

slide-58
SLIDE 58

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small)) (print ) The begin special form combines multiple expressions into one expression if x > 10: print('big') print('guy') else: print('small') print('fry')

slide-59
SLIDE 59

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small)) (print ) The begin special form combines multiple expressions into one expression if x > 10: print('big') print('guy') else: print('small') print('fry') (cond ((> x 10) (begin (print 'big) (print 'guy))) (else (begin (print 'small) (print 'fry))))

slide-60
SLIDE 60

Cond & Begin

The cond special form that behaves like if-elif-else statements in Python

13

if x > 10: print('big') elif x > 5: print('medium') else: print('small') (cond ((> x 10) (print 'big)) ((> x 5) (print 'medium)) (else (print 'small))) (cond ((> x 10) 'big) ((> x 5) 'medium) (else 'small)) (print ) The begin special form combines multiple expressions into one expression if x > 10: print('big') print('guy') else: print('small') print('fry') (cond ((> x 10) (begin (print 'big) (print 'guy))) (else (begin (print 'small) (print 'fry)))) (if (> x 10) (begin (print 'big) (print 'guy)) (begin (print 'small) (print 'fry)))

slide-61
SLIDE 61

Let Expressions

The let special form binds symbols to values temporarily; just for one expression

14

slide-62
SLIDE 62

Let Expressions

The let special form binds symbols to values temporarily; just for one expression

14

a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b)

slide-63
SLIDE 63

Let Expressions

The let special form binds symbols to values temporarily; just for one expression

14

a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b) a and b are still bound down here

slide-64
SLIDE 64

Let Expressions

The let special form binds symbols to values temporarily; just for one expression

14

a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b) (define c (let ((a 3) (b (+ 2 2))) (sqrt (+ (* a a) (* b b))))) a and b are still bound down here

slide-65
SLIDE 65

Let Expressions

The let special form binds symbols to values temporarily; just for one expression

14

a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b) (define c (let ((a 3) (b (+ 2 2))) (sqrt (+ (* a a) (* b b))))) a and b are still bound down here a and b are not bound down here

slide-66
SLIDE 66

Lists

slide-67
SLIDE 67

Scheme Lists

slide-68
SLIDE 68

Scheme Lists

In the late 1950s, computer scientists used confusing names

slide-69
SLIDE 69

Scheme Lists

In the late 1950s, computer scientists used confusing names

  • cons: Two-argument procedure that creates a linked list
slide-70
SLIDE 70

Scheme Lists

In the late 1950s, computer scientists used confusing names

  • cons: Two-argument procedure that creates a linked list
  • car: Procedure that returns the first element of a list
slide-71
SLIDE 71

Scheme Lists

In the late 1950s, computer scientists used confusing names

  • cons: Two-argument procedure that creates a linked list
  • car: Procedure that returns the first element of a list
  • cdr: Procedure that returns the rest of a list
slide-72
SLIDE 72

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

(cons 2 nil)

2 nil

slide-74
SLIDE 74

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

2

(cons 2 nil)

2 nil

slide-75
SLIDE 75

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces

2

(cons 2 nil)

2 nil

slide-76
SLIDE 76

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces

2

(cons 2 nil)

(cons 2 nil)

2 nil 2

slide-77
SLIDE 77

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces >

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-78
SLIDE 78

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-79
SLIDE 79

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil))

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-80
SLIDE 80

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-81
SLIDE 81

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-82
SLIDE 82

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-83
SLIDE 83

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-84
SLIDE 84

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-85
SLIDE 85

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-86
SLIDE 86

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil))))

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 2

slide-87
SLIDE 87

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil))))

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 1 2 3 4 2

slide-88
SLIDE 88

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 1 2 3 4 2

slide-89
SLIDE 89

Scheme Lists

In the late 1950s, computer scientists used confusing names

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

Important! Scheme lists are written in parentheses with elements separated by spaces > (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4)

(Demo)

2

(cons 1 ) (cons 2 nil)

1

(cons 2 nil)

2 nil 1 2 3 4 2

slide-90
SLIDE 90

Symbolic Programming

slide-91
SLIDE 91

Symbolic Programming

18

slide-92
SLIDE 92

Symbolic Programming

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

18

slide-93
SLIDE 93

Symbolic Programming

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

18

slide-94
SLIDE 94

Symbolic Programming

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

18

slide-95
SLIDE 95

Symbolic Programming

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

18

slide-96
SLIDE 96

Symbolic Programming

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

18

slide-97
SLIDE 97

Symbolic Programming

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

18

slide-98
SLIDE 98

Symbolic Programming

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

18

slide-99
SLIDE 99

Symbolic Programming

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

18

slide-100
SLIDE 100

Symbolic Programming

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

18

slide-101
SLIDE 101

Symbolic Programming

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

18

slide-102
SLIDE 102

Symbolic Programming

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

18

slide-103
SLIDE 103

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-104
SLIDE 104

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-105
SLIDE 105

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-106
SLIDE 106

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-107
SLIDE 107

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) > (car '(a b c)) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-108
SLIDE 108

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) > (car '(a b c)) a Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-109
SLIDE 109

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) > (car '(a b c)) a > (cdr '(a b c)) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-110
SLIDE 110

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) > (car '(a b c)) a > (cdr '(a b c)) (b c) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

slide-111
SLIDE 111

Symbolic Programming

Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) Quotation is used to refer to symbols directly in Lisp. No sign of “a” and “b” in the resulting value > (list 'a 'b) (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > '(a b c) (a b c) > (car '(a b c)) a > (cdr '(a b c)) (b c) Short for (quote a), (quote b): Special form to indicate that the expression itself is the value.

18

(Demo)

slide-112
SLIDE 112

Programs as Data

slide-113
SLIDE 113

A Scheme Expression is a Scheme List

20

slide-114
SLIDE 114

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

20

slide-115
SLIDE 115

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

  • Primitive expressions: 2 3.3 true + quotient

20

slide-116
SLIDE 116

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

slide-117
SLIDE 117

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-118
SLIDE 118

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-119
SLIDE 119

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-120
SLIDE 120

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-121
SLIDE 121

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-122
SLIDE 122

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) 5 The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-123
SLIDE 123

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) 5 The built-in Scheme list data structure (which is a linked list) can represent combinations

slide-124
SLIDE 124

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) 5 The built-in Scheme list data structure (which is a linked list) can represent combinations In such a language, it is straightforward to write a program that writes a program

slide-125
SLIDE 125

A Scheme Expression is a Scheme List

Scheme programs consist of expressions, which can be:

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

20

scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) 5 (Demo) The built-in Scheme list data structure (which is a linked list) can represent combinations In such a language, it is straightforward to write a program that writes a program

slide-126
SLIDE 126

Generating Code

slide-127
SLIDE 127

Quasiquotation

22

slide-128
SLIDE 128

Quasiquotation

There are two ways to quote an expression

22

slide-129
SLIDE 129

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b)

22

slide-130
SLIDE 130

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b)

22

slide-131
SLIDE 131

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with ,

22

slide-132
SLIDE 132

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4)

22

slide-133
SLIDE 133

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4) Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1))

22

slide-134
SLIDE 134

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4) Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1)) Quasiquote: `(a ,(+ b 1)) => (a 5)

22

slide-135
SLIDE 135

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4) Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1)) Quasiquote: `(a ,(+ b 1)) => (a 5) Quasiquotation is particularly convenient for generating Scheme expressions:

22

slide-136
SLIDE 136

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4) Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1)) Quasiquote: `(a ,(+ b 1)) => (a 5) Quasiquotation is particularly convenient for generating Scheme expressions: (define (make-add-procedure n) `(lambda (d) (+ d ,n)))

22

slide-137
SLIDE 137

Quasiquotation

There are two ways to quote an expression Quote: '(a b) => (a b) Quasiquote: `(a b) => (a b) They are different because parts of a quasiquoted expression can be unquoted with , (define b 4) Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1)) Quasiquote: `(a ,(+ b 1)) => (a 5) Quasiquotation is particularly convenient for generating Scheme expressions: (define (make-add-procedure n) `(lambda (d) (+ d ,n))) (make-add-procedure 2) => (lambda (d) (+ d 2))

22

slide-138
SLIDE 138

Example: While Statements

23

slide-139
SLIDE 139

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

slide-140
SLIDE 140

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2

slide-141
SLIDE 141

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total))

slide-142
SLIDE 142

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (f 2 0))

slide-143
SLIDE 143

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (begin ) (f 2 0))

slide-144
SLIDE 144

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (begin ) (f 2 0)) What's the sum of the numbers whose squares are less than 50, starting with 1?

slide-145
SLIDE 145

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (begin ) (f 2 0)) x = 1 total = 0 while x * x < 50: total = total + x x = x + 1 What's the sum of the numbers whose squares are less than 50, starting with 1?

slide-146
SLIDE 146

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (begin ) (f 2 0)) x = 1 total = 0 while x * x < 50: total = total + x x = x + 1 (define (f x total) (if (< (* x x) 50) (f (+ x 1) (+ total x)) total)) (begin ) (f 1 0)) What's the sum of the numbers whose squares are less than 50, starting with 1?

slide-147
SLIDE 147

Example: While Statements

What's the sum of the squares of even numbers less than 10, starting with 2?

23

x = 2 total = 0 while x < 10: total = total + x * x x = x + 2 (define (f x total) (if (< x 10) (f (+ x 2) (+ total (* x x))) total)) (begin ) (f 2 0)) x = 1 total = 0 while x * x < 50: total = total + x x = x + 1 (define (f x total) (if (< (* x x) 50) (f (+ x 1) (+ total x)) total)) (begin ) (f 1 0)) What's the sum of the numbers whose squares are less than 50, starting with 1? (Demo)