CS 61A Discussion 9 Scheme Albert Xu Attendance: - - PowerPoint PPT Presentation

cs 61a discussion 9
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 9 Scheme Albert Xu Attendance: - - PowerPoint PPT Presentation

CS 61A Discussion 9 Scheme Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Major Differences Scheme has a fundamental philosophical di ff erence from languages like Python. Major Differences


slide-1
SLIDE 1

CS 61A Discussion 9

Scheme Albert Xu

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Major Differences

Scheme has a fundamental philosophical difference from languages like Python.

slide-4
SLIDE 4

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. expression statement

slide-5
SLIDE 5

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 expression statement

slide-6
SLIDE 6

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement

slide-7
SLIDE 7

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement Scheme does not have statements! Everything is evaluated into a value.

slide-8
SLIDE 8

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement Scheme does not have statements! Everything is evaluated into a value.

in Scheme…

slide-9
SLIDE 9

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement Scheme does not have statements! Everything is evaluated into a value. expression statement

in Scheme…

slide-10
SLIDE 10

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement Scheme does not have statements! Everything is evaluated into a value. (+ 2 3) expression statement

in Scheme…

slide-11
SLIDE 11

Major Differences

Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement Scheme does not have statements! Everything is evaluated into a value. (+ 2 3) expression statement

in Scheme…

slide-12
SLIDE 12

Primitives and Expressions

scheme doesn’t have statements!

primitives

slide-13
SLIDE 13

Primitives and Expressions

scheme doesn’t have statements!

primitives 1 1.234

slide-14
SLIDE 14

Primitives and Expressions

scheme doesn’t have statements!

primitives 1 ‘abc 1.234

slide-15
SLIDE 15

Primitives and Expressions

scheme doesn’t have statements!

primitives 1 ‘abc 1.234 x

slide-16
SLIDE 16

Primitives and Expressions

scheme doesn’t have statements!

primitives 1 ‘abc #t #f 1.234 x

slide-17
SLIDE 17

Primitives and Expressions

scheme doesn’t have statements!

primitives 1 ‘abc #t #f 1.234 a.k.a. atoms x

slide-18
SLIDE 18

Primitives and Expressions

scheme doesn’t have statements!

primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms x

slide-19
SLIDE 19

Primitives and Expressions

scheme doesn’t have statements!

primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (define a 1) x

slide-20
SLIDE 20

Primitives and Expressions

scheme doesn’t have statements!

primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (+ 2 3) (define a 1) x

slide-21
SLIDE 21

Primitives and Expressions

scheme doesn’t have statements!

primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (+ 2 3) (define a 1) (lambda (x) (+ x 1)) x

slide-22
SLIDE 22

Primitives and Expressions

scheme doesn’t have statements!

primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (+ 2 3) (define a 1) (lambda (x) (+ x 1))

notice that all expressions are formed by combining atoms!

x

slide-23
SLIDE 23

define expression

Two ways to use define! 1. Defining variables 2. Defining functions

slide-24
SLIDE 24

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

slide-25
SLIDE 25

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1)

slide-26
SLIDE 26

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc

slide-27
SLIDE 27

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc

slide-28
SLIDE 28

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1

slide-29
SLIDE 29

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc

slide-30
SLIDE 30

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc

slide-31
SLIDE 31

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc

slide-32
SLIDE 32

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc)

slide-33
SLIDE 33

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1

slide-34
SLIDE 34

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-35
SLIDE 35

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1))

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-36
SLIDE 36

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-37
SLIDE 37

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-38
SLIDE 38

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1))

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-39
SLIDE 39

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1)

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-40
SLIDE 40

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-41
SLIDE 41

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

(define (<fn> <op-1> … <op-n>) (fn-body) )

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2 >>> (eval ‘(f 1))

slide-42
SLIDE 42

define expression

Two ways to use define!

(define <symbol> <value>)

1. Defining variables 2. Defining functions

>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc) 1 >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2 >>> (eval ‘(f 1)) 2

(define (<fn> <op-1> … <op-n>) (fn-body) )

slide-43
SLIDE 43

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>)

slide-44
SLIDE 44

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

slide-45
SLIDE 45

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions? 1) Evaluate the operator 2) Evaluate the operands

slide-46
SLIDE 46

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions? 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-47
SLIDE 47

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1))

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-48
SLIDE 48

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-49
SLIDE 49

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-50
SLIDE 50

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1))

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-51
SLIDE 51

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1)

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-52
SLIDE 52

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-53
SLIDE 53

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2 >>> (f 1 2)

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-54
SLIDE 54

Call Expressions, again

Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1)) >>> (f 1) 2 >>> (f 1 2) Error: Too many arguments …

1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)

slide-55
SLIDE 55

Special Forms

unlike call expressions…

slide-56
SLIDE 56

What is a Special Form?

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do!

slide-57
SLIDE 57

What is a Special Form?

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-58
SLIDE 58

What is a Special Form?

Examples

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-59
SLIDE 59

What is a Special Form?

define is a special form!

Examples

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-60
SLIDE 60

What is a Special Form?

define is a special form!

Examples

(define x 1)

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-61
SLIDE 61

What is a Special Form?

define is a special form! Short-circuiting means that we can’t evaluate an operand until we know the truth value of the previous operand.

Examples

(define x 1)

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-62
SLIDE 62

What is a Special Form?

define is a special form! Short-circuiting means that we can’t evaluate an operand until we know the truth value of the previous operand.

Examples

(define x 1) (or 2 (/ 1 0)

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-63
SLIDE 63

What is a Special Form?

define is a special form! Short-circuiting means that we can’t evaluate an operand until we know the truth value of the previous operand. if expressions should only evaluate the <if-true> or <if-false> operand

Examples

(define x 1) (or 2 (/ 1 0)

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-64
SLIDE 64

What is a Special Form?

define is a special form! Short-circuiting means that we can’t evaluate an operand until we know the truth value of the previous operand. if expressions should only evaluate the <if-true> or <if-false> operand

Examples

(define x 1) (or 2 (/ 1 0) (if #t (+ 1 2) (- 2 1))

There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the

  • perands in a different order, or don’t evaluate them at all!
slide-65
SLIDE 65

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy!

slide-66
SLIDE 66

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1

slide-67
SLIDE 67

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1

slide-68
SLIDE 68

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

slide-69
SLIDE 69

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

slide-70
SLIDE 70

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

slide-71
SLIDE 71

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

slide-72
SLIDE 72

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f

slide-73
SLIDE 73

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f

slide-74
SLIDE 74

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil

slide-75
SLIDE 75

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil

slide-76
SLIDE 76

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

slide-77
SLIDE 77

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

slide-78
SLIDE 78

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t)

slide-79
SLIDE 79

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t) x

slide-80
SLIDE 80

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0))

slide-81
SLIDE 81

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0)) 2

slide-82
SLIDE 82

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0)) 2 >>> (if 0 (/ 1 0) (+ 1 1))

slide-83
SLIDE 83

if, booleans

Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form

  • Syntax: (if <condition>

<if-true> <if-false>)

  • If <condition> is truthy,

eval and return <if-true>. Otherwise eval and return <if-false>.

>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0)) 2 >>> (if 0 (/ 1 0) (+ 1 1)) Error: Cannot divide by 0

slide-84
SLIDE 84

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f)

slide-85
SLIDE 85

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1

slide-86
SLIDE 86

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3)

slide-87
SLIDE 87

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f

slide-88
SLIDE 88

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f >>> (and 2 3)

slide-89
SLIDE 89

short circuiting

Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f >>> (and 2 3) 3

slide-90
SLIDE 90

lambdas are here to stay

Convert this Python code to Scheme:

square = lambda x: x**2

slide-91
SLIDE 91

lambdas are here to stay

Convert this Python code to Scheme:

square = lambda x: x**2 (define square (lambda (x) (* x x)))

slide-92
SLIDE 92

lambdas are here to stay

All functions in Scheme are lambda functions! Convert this Python code to Scheme:

square = lambda x: x**2 (define square (lambda (x) (* x x)))

slide-93
SLIDE 93

lambdas are here to stay

>>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1))

All functions in Scheme are lambda functions! Convert this Python code to Scheme:

square = lambda x: x**2 (define square (lambda (x) (* x x)))

slide-94
SLIDE 94

Lists in Scheme

remember linked lists?

slide-95
SLIDE 95

List Basics

  • Scheme lists are like Python’s Pair. Both are linked lists.
  • Attributes
  • first - the current list element
  • rest - the rest of the elements
slide-96
SLIDE 96

List Basics

  • Scheme lists are like Python’s Pair. Both are linked lists.
  • Attributes
  • first - the current list element
  • rest - the rest of the elements
  • Accessing List Values
  • car - get the first element of a list
  • cdr - get the rest of a list
slide-97
SLIDE 97

List Basics

  • Scheme lists are like Python’s Pair. Both are linked lists.
  • Attributes
  • first - the current list element
  • rest - the rest of the elements
  • Accessing List Values
  • car - get the first element of a list
  • cdr - get the rest of a list

1 4 9

first rest

Pair(1, Pair(4, Pair(9, nil)))

slide-98
SLIDE 98

List Basics

  • Scheme lists are like Python’s Pair. Both are linked lists.
  • Attributes
  • first - the current list element
  • rest - the rest of the elements
  • Accessing List Values
  • car - get the first element of a list
  • cdr - get the rest of a list

1 4 9

first rest

(cons 1 (cons 4 (cons 9 nil))) Pair(1, Pair(4, Pair(9, nil)))

slide-99
SLIDE 99

Equality

in scheme = can only be used for comparing numbers.

eq? behaves like == in Python for comparing two non-pairs (numbers, booleans, etc.). Otherwise, eq? behaves like is in Python (= 2 2) (= ‘(1) ‘(1)) (eq? 2 2) (eq? #t #t) (eq? ‘(1) ‘(1)) (define lst ‘(1)) (eq? lst lst)

slide-100
SLIDE 100

Equality

in scheme = can only be used for comparing numbers.

(= 2 2) (= ‘(1) ‘(1))

slide-101
SLIDE 101

Equality

in scheme = can only be used for comparing numbers.

eq? behaves like == in Python for comparing two non-pairs (numbers, booleans, etc.). Otherwise, eq? behaves like is in Python (= 2 2) (= ‘(1) ‘(1))

equal? compares pairs by determining if their cars are equal? and their cdrs are equal?(that is, they have the same contents). Otherwise, equal? behaves like eq?

(eq? 2 2) (eq? #t #t) (eq? ‘(1) ‘(1)) (define lst ‘(1)) (eq? lst lst) (equal? ‘(1) ‘(1)) (define lst ‘(1)) (equal? lst lst)

slide-102
SLIDE 102

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list 1

(2 3)

slide-103
SLIDE 103

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3)) (1 2 3)

slide-104
SLIDE 104

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3))

Given: multiple elements

1 2 3 (1 2 3)

slide-105
SLIDE 105

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3))

Given: multiple elements Use: list

1 2 3 (list 1 2 3) (1 2 3)

slide-106
SLIDE 106

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3))

Given: multiple elements Use: list

1 2 3 (list 1 2 3)

Given: two lists

(1 2 3) (2 3) (1)

slide-107
SLIDE 107

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3))

Given: multiple elements Use: list

1 2 3 (list 1 2 3)

Given: two lists Use: append

(append ‘(1) ‘(2 3)) (1 2 3) (2 3) (1)

slide-108
SLIDE 108

Creating Lists

One of the most important things about solving Scheme problems is knowing what function you should use to create your list! Given: an element and a list Use: cons

1 (2 3) (cons 1 (2 3))

Given: multiple elements Use: list

1 2 3 (list 1 2 3)

Given: two lists Use: append

(append ‘(1) ‘(2 3)) (1 2 3) (2 3) (1)

*cons is the most powerful and basic function! use it whenever possible…

slide-109
SLIDE 109

Tips for Solving Scheme Questions

  • Draw box-and-pointer examples to help visualize the problem when needed.
  • Problems will require recursive solutions because Scheme has no iteration
  • When writing recursive Scheme functions:
  • Think about your base case (use if or cond).
  • Think about using recursion on the cdr of the list.
  • Think about how you would approach the problem in Python. Both

languages are well suited for functional programming.

  • Helpful functions:
  • (list args) – takes 1 or more arguments and returns a well-formed list
  • (null? arg) – checks if a given element is an empty list
  • (append list1 list2) – takes 2 lists and concatenates them into one list
slide-110
SLIDE 110

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/