Lecture 19: Scheme I Marvin Zhang 07/25/2016 Announcements - - PowerPoint PPT Presentation

lecture 19 scheme i
SMART_READER_LITE
LIVE PREVIEW

Lecture 19: Scheme I Marvin Zhang 07/25/2016 Announcements - - PowerPoint PPT Presentation

Lecture 19: Scheme I Marvin Zhang 07/25/2016 Announcements Roadmap Introduction Functions Data Mutability Objects Interpretation Paradigms Applications Roadmap Introduction Functions This week (Interpretation), the goals are:


slide-1
SLIDE 1

Marvin Zhang 07/25/2016

Lecture 19: Scheme I

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Roadmap Introduction Functions Data Mutability Objects Interpretation Paradigms Applications

slide-4
SLIDE 4

Roadmap

  • This week (Interpretation), the

goals are:

Introduction Functions Data Mutability Objects Interpretation Paradigms Applications

slide-5
SLIDE 5

Roadmap

  • This week (Interpretation), the

goals are:

  • To learn a new language, Scheme,

in two days!

Introduction Functions Data Mutability Objects Interpretation Paradigms Applications

slide-6
SLIDE 6

Roadmap

  • This week (Interpretation), the

goals are:

  • To learn a new language, Scheme,

in two days!

  • To understand how interpreters

work, using Scheme as an example

Introduction Functions Data Mutability Objects Interpretation Paradigms Applications

slide-7
SLIDE 7

Scheme

slide-8
SLIDE 8

Scheme

  • Scheme is a dialect of Lisp, the second-oldest language

still used today

slide-9
SLIDE 9

Scheme

  • Scheme is a dialect of Lisp, the second-oldest language

still used today

  • “If you don't know Lisp, you don't know what it means for a

programming language to be powerful and elegant.”

  • Richard Stallman, creator of Emacs
slide-10
SLIDE 10

Scheme

  • Scheme is a dialect of Lisp, the second-oldest language

still used today

  • “If you don't know Lisp, you don't know what it means for a

programming language to be powerful and elegant.”

  • Richard Stallman, creator of Emacs
  • “The greatest single programming language ever designed.”
  • Alan Kay, co-creator of OOP
slide-11
SLIDE 11

Scheme

  • Scheme is a dialect of Lisp, the second-oldest language

still used today

  • “If you don't know Lisp, you don't know what it means for a

programming language to be powerful and elegant.”

  • Richard Stallman, creator of Emacs
  • “The greatest single programming language ever designed.”
  • Alan Kay, co-creator of OOP
  • Lisp is known for its simple but powerful syntax, and its

ridiculous number of parentheses

slide-12
SLIDE 12

Scheme

  • Scheme is a dialect of Lisp, the second-oldest language

still used today

  • “If you don't know Lisp, you don't know what it means for a

programming language to be powerful and elegant.”

  • Richard Stallman, creator of Emacs
  • “The greatest single programming language ever designed.”
  • Alan Kay, co-creator of OOP
  • Lisp is known for its simple but powerful syntax, and its

ridiculous number of parentheses

  • What does Lisp stand for?
slide-13
SLIDE 13

Scheme Fundamentals

slide-14
SLIDE 14

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
slide-15
SLIDE 15

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
slide-16
SLIDE 16

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
  • There are various ways to combine primitives into more

complex expressions

slide-17
SLIDE 17

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
  • There are various ways to combine primitives into more

complex expressions

  • Call expressions include an operator followed by zero
  • r more operands, all surrounded by parentheses
slide-18
SLIDE 18

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
  • There are various ways to combine primitives into more

complex expressions

  • Call expressions include an operator followed by zero
  • r more operands, all surrounded by parentheses

(demo)

slide-19
SLIDE 19

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
  • There are various ways to combine primitives into more

complex expressions

  • Call expressions include an operator followed by zero
  • r more operands, all surrounded by parentheses

scm> (quotient (+ 8 7) 5) 3

(demo)

slide-20
SLIDE 20

Scheme Fundamentals

  • Scheme primitives include numbers, Booleans, and symbols
  • More on symbols later (for now, they’re like variables)
  • There are various ways to combine primitives into more

complex expressions

  • Call expressions include an operator followed by zero
  • r more operands, all surrounded by parentheses

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

(demo)

slide-21
SLIDE 21

Assignment, Symbols, Functions, and Conditionals

Special Forms

slide-22
SLIDE 22

Assignment Statements

slide-23
SLIDE 23

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
slide-24
SLIDE 24

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
slide-25
SLIDE 25

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

slide-26
SLIDE 26

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5)

slide-27
SLIDE 27

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a

slide-28
SLIDE 28

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a

slide-29
SLIDE 29

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5

slide-30
SLIDE 30

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4))

slide-31
SLIDE 31

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b

slide-32
SLIDE 32

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b scm> b

slide-33
SLIDE 33

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b scm> b 9

slide-34
SLIDE 34

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b scm> b 9

Expressions

slide-35
SLIDE 35

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

  • Everything in Scheme is an expression, meaning everything

evaluates to a value

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b scm> b 9

Expressions

slide-36
SLIDE 36

Assignment Statements

  • Special forms in Scheme have special orders of evaluation
  • We can bind symbols to values using define
  • (define <symbol> <expression>) binds <symbol> to the

value that <expression> evaluates to

  • Everything in Scheme is an expression, meaning everything

evaluates to a value

  • define expressions evaluate to the symbol that was bound

scm> (define a 5) a scm> a 5 scm> (define b (+ a 4)) b scm> b 9

Expressions

slide-37
SLIDE 37

Symbols and quote

slide-38
SLIDE 38

Symbols and quote

  • Symbols are like variables, they can be bound to values
slide-39
SLIDE 39

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

slide-40
SLIDE 40

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
slide-41
SLIDE 41

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

slide-42
SLIDE 42

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5)

slide-43
SLIDE 43

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a

slide-44
SLIDE 44

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a

slide-45
SLIDE 45

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a 5

slide-46
SLIDE 46

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a 5 scm> (quote a)

slide-47
SLIDE 47

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a 5 scm> (quote a) a

slide-48
SLIDE 48

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a 5 scm> (quote a) a scm> 'a ; shorthand for (quote a)

slide-49
SLIDE 49

Symbols and quote

  • Symbols are like variables, they can be bound to values
  • However, unlike variables, they also exist on their own

as their own values

  • Symbols are like strings and variables all in one
  • We can reference symbols directly, rather than the value

they are bound to, using the quote special form

scm> (define a 5) a scm> a 5 scm> (quote a) a scm> 'a ; shorthand for (quote a) a

slide-50
SLIDE 50

Assignment Expressions

slide-51
SLIDE 51

Assignment Expressions

  • define expressions evaluate to the symbol that was bound,

not the value the symbol was bound to

slide-52
SLIDE 52

Assignment Expressions

  • define expressions evaluate to the symbol that was bound,

not the value the symbol was bound to

  • The side effect of a define expression is to bind the

symbol to the value of the expression

slide-53
SLIDE 53

Assignment Expressions

  • define expressions evaluate to the symbol that was bound,

not the value the symbol was bound to

  • The side effect of a define expression is to bind the

symbol to the value of the expression

(demo)

slide-54
SLIDE 54

Assignment Expressions

  • define expressions evaluate to the symbol that was bound,

not the value the symbol was bound to

  • The side effect of a define expression is to bind the

symbol to the value of the expression

scm> (define a 5) a scm> (define b a) b scm> b 5 scm> (define c (define a 3)) c scm> a 3 scm> c a

(demo)

slide-55
SLIDE 55

Lambda Expressions

slide-56
SLIDE 56

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
slide-57
SLIDE 57

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

slide-58
SLIDE 58

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

slide-59
SLIDE 59

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

  • perator
slide-60
SLIDE 60

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

  • perator
  • perand
slide-61
SLIDE 61

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

  • More commonly, we can bind it to a symbol using an

assignment, e.g., (define square (lambda (x) (* x x)))

  • perator
  • perand
slide-62
SLIDE 62

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

  • More commonly, we can bind it to a symbol using an

assignment, e.g., (define square (lambda (x) (* x x)))

  • This is so common that we have a shorthand for this:

(define (square x) (* x x)) does the exact same thing

  • perator
  • perand
slide-63
SLIDE 63

Lambda Expressions

  • lambda expressions evaluate to anonymous procedures
  • (lambda (<parameters>) <body>) creates a procedure as

the side effect, and evaluates to the procedure itself

  • We can use the procedure directly as the operator in a

call expression, e.g., ((lambda (x) (* x x)) 4)

  • More commonly, we can bind it to a symbol using an

assignment, e.g., (define square (lambda (x) (* x x)))

  • This is so common that we have a shorthand for this:

(define (square x) (* x x)) does the exact same thing

  • This looks like a Python def statement, but the

procedure it creates is still anonymous!

  • perator
  • perand
slide-64
SLIDE 64

Conditionals and Booleans

slide-65
SLIDE 65

Conditionals and Booleans

  • Conditional expressions come in two types:
slide-66
SLIDE 66

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

slide-67
SLIDE 67

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

  • We can chain conditionals together similar to Python


if-elif-else statements using the cond expression

slide-68
SLIDE 68

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

  • We can chain conditionals together similar to Python


if-elif-else statements using the cond expression

(demo)

slide-69
SLIDE 69

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

  • We can chain conditionals together similar to Python


if-elif-else statements using the cond expression

scm> (cond ((= 3 4) 4) ((= 3 3) 0) (else 'hi))

(demo)

slide-70
SLIDE 70

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

  • We can chain conditionals together similar to Python


if-elif-else statements using the cond expression

  • Booleans expressions (and <e1> … <en>), (or <e1> … <en>)

short-circuit just like Python Boolean expressions

scm> (cond ((= 3 4) 4) ((= 3 3) 0) (else 'hi))

(demo)

slide-71
SLIDE 71

Conditionals and Booleans

  • Conditional expressions come in two types:
  • (if <predicate> <consequent> <alternative>) evaluates

<predicate>, and then evaluates and returns the value of either <consequent> or <alternative>

  • We can chain conditionals together similar to Python


if-elif-else statements using the cond expression

  • Booleans expressions (and <e1> … <en>), (or <e1> … <en>)

short-circuit just like Python Boolean expressions

  • In Scheme, only #f (and false, and False) are false values!

scm> (cond ((= 3 4) 4) ((= 3 3) 0) (else 'hi))

(demo)

slide-72
SLIDE 72

Scheme data structures

Pairs and Lists

slide-73
SLIDE 73

Pairs and Lists

slide-74
SLIDE 74

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
slide-75
SLIDE 75

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

slide-76
SLIDE 76

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

slide-77
SLIDE 77

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3))

slide-78
SLIDE 78

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x

slide-79
SLIDE 79

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x

slide-80
SLIDE 80

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x (1 . 3)

slide-81
SLIDE 81

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x (1 . 3) scm> (car x)

slide-82
SLIDE 82

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x (1 . 3) scm> (car x) 1

slide-83
SLIDE 83

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x (1 . 3) scm> (car x) 1 scm> (cdr x)

slide-84
SLIDE 84

Pairs and Lists

  • Disclaimer: programmers in the 1950s used confusing terms
  • The pair is the basic compound value in Scheme, and is

constructed using a cons expression

  • car selects the first element in a pair, and cdr selects

the second element

scm> (define x (cons 1 3)) x scm> x (1 . 3) scm> (car x) 1 scm> (cdr x) 3

slide-85
SLIDE 85

Pairs and Lists

slide-86
SLIDE 86

Pairs and Lists

  • The only type of sequence in Scheme is the linked list,

which we can create using just pairs!

slide-87
SLIDE 87

Pairs and Lists

  • The only type of sequence in Scheme is the linked list,

which we can create using just pairs!

  • There is also shorthand for creating linked lists using

the list expression

slide-88
SLIDE 88

Pairs and Lists

  • The only type of sequence in Scheme is the linked list,

which we can create using just pairs!

  • There is also shorthand for creating linked lists using

the list expression

  • nil represents the empty list
slide-89
SLIDE 89

Pairs and Lists

  • The only type of sequence in Scheme is the linked list,

which we can create using just pairs!

  • There is also shorthand for creating linked lists using

the list expression

  • nil represents the empty list

(demo)

slide-90
SLIDE 90

Pairs and Lists

  • The only type of sequence in Scheme is the linked list,

which we can create using just pairs!

  • There is also shorthand for creating linked lists using

the list expression

  • nil represents the empty list

(demo)

scm> (define x (cons 1 (cons 2 (cons 3 nil)))) x scm> x ; no dots displayed for well-formed lists (1 2 3) scm> (car x) 1 scm> (cdr x) (2 3) scm> (list 1 2 3) ; shorthand (1 2 3) scm> '(1 2 3) ; shortest-hand (1 2 3)

slide-91
SLIDE 91

Coding Practice

slide-92
SLIDE 92

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
slide-93
SLIDE 93

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

slide-94
SLIDE 94

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

  • A good way to start these problems is to write it in

Python first, using linked lists and recursion

slide-95
SLIDE 95

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

  • A good way to start these problems is to write it in

Python first, using linked lists and recursion

  • Usually pretty easy to translate to Scheme afterwards
slide-96
SLIDE 96

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

  • A good way to start these problems is to write it in

Python first, using linked lists and recursion

  • Usually pretty easy to translate to Scheme afterwards
  • Basic versions of Scheme don’t have iteration!
slide-97
SLIDE 97

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

  • A good way to start these problems is to write it in

Python first, using linked lists and recursion

  • Usually pretty easy to translate to Scheme afterwards
  • Basic versions of Scheme don’t have iteration!

(demo)

slide-98
SLIDE 98

Coding Practice

  • Let’s implement a procedure (map fn lst), where fn is a
  • ne-element procedure and lst is a (linked) list
  • (map fn lst) returns a new (linked) list with fn

applied to all of the elements in lst

  • A good way to start these problems is to write it in

Python first, using linked lists and recursion

  • Usually pretty easy to translate to Scheme afterwards
  • Basic versions of Scheme don’t have iteration!

(demo)

(define (map fn lst) (if (null? lst) nil (cons (fn (car lst)) (map fn (cdr lst)))))

slide-99
SLIDE 99

More Coding Practice

slide-100
SLIDE 100

More Coding Practice

  • We can create a tree abstraction just like in Python:
slide-101
SLIDE 101

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children)

slide-102
SLIDE 102

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children))

slide-103
SLIDE 103

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree))

slide-104
SLIDE 104

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree)) (define (children tree) (cdr tree))

slide-105
SLIDE 105

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree)) (define (children tree) (cdr tree)) (define (leaf? tree)

slide-106
SLIDE 106

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree)) (define (children tree) (cdr tree)) (define (leaf? tree) (null? (children tree)))

slide-107
SLIDE 107

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree)) (define (children tree) (cdr tree)) (define (leaf? tree) (null? (children tree)))

(demo)

slide-108
SLIDE 108

More Coding Practice

  • We can create a tree abstraction just like in Python:

(define (tree entry children) (cons entry children)) (define (entry tree) (car tree)) (define (children tree) (cdr tree)) (define (leaf? tree) (null? (children tree)))

(demo)

(define (square-tree t) (tree (square (entry t)) (if (leaf? t) nil (map square-tree (children t)))))

slide-109
SLIDE 109

Summary

slide-110
SLIDE 110

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

slide-111
SLIDE 111

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

  • Scheme is a simpler language, but still very powerful
slide-112
SLIDE 112

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

  • Scheme is a simpler language, but still very powerful
  • Everything in Scheme is an expression
slide-113
SLIDE 113

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

  • Scheme is a simpler language, but still very powerful
  • Everything in Scheme is an expression
  • All functions (called procedures) are anonymous
slide-114
SLIDE 114

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

  • Scheme is a simpler language, but still very powerful
  • Everything in Scheme is an expression
  • All functions (called procedures) are anonymous
  • Because the only sequence is the linked list, we will

solve problems using recursion

slide-115
SLIDE 115

Summary

  • We learned a new language today! Being able to quickly

pick up new languages is important for good programmers

  • Scheme is a simpler language, but still very powerful
  • Everything in Scheme is an expression
  • All functions (called procedures) are anonymous
  • Because the only sequence is the linked list, we will

solve problems using recursion

  • “How do I master Scheme?” Go practice!