CS 61A Discussion 9
Scheme Albert Xu
Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/
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
Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/
Scheme has a fundamental philosophical difference from languages like Python.
Scheme has a fundamental philosophical difference from languages like Python. expression statement
Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 expression statement
Scheme has a fundamental philosophical difference from languages like Python. 2 + 3 def f(x): return x expression statement
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.
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…
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…
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…
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…
scheme doesn’t have statements!
primitives
scheme doesn’t have statements!
primitives 1 1.234
scheme doesn’t have statements!
primitives 1 ‘abc 1.234
scheme doesn’t have statements!
primitives 1 ‘abc 1.234 x
scheme doesn’t have statements!
primitives 1 ‘abc #t #f 1.234 x
scheme doesn’t have statements!
primitives 1 ‘abc #t #f 1.234 a.k.a. atoms x
scheme doesn’t have statements!
primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms x
scheme doesn’t have statements!
primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (define a 1) x
scheme doesn’t have statements!
primitives expressions 1 ‘abc #t #f 1.234 a.k.a. atoms (+ 2 3) (define a 1) x
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
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
Two ways to use define! 1. Defining variables 2. Defining functions
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1)
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc 1
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc 1 >>> ‘abc
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc
Two ways to use define!
(define <symbol> <value>)
1. Defining variables 2. Defining functions
>>> (define abc 1) abc >>> abc 1 >>> ‘abc abc >>> (eval ‘abc)
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
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) )
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) )
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) )
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) )
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) )
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) )
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) )
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))
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) )
Syntax: (<operator> <op-1> <op-2> … <op-n>)
Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions?
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
unlike call expressions…
There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do!
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
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
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
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
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
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
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
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
Remember! The only falsy value in Scheme is #f, everything else is truthy!
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
eval and return <if-true>. Otherwise eval and return <if-false>.
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
eval and return <if-true>. Otherwise eval and return <if-false>.
>>> (define x #t)
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
eval and return <if-true>. Otherwise eval and return <if-false>.
>>> (define x #t) x
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
eval and return <if-true>. Otherwise eval and return <if-false>.
>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0))
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
eval and return <if-true>. Otherwise eval and return <if-false>.
>>> (define x #t) x >>> (if x (+ 1 1) (/ 1 0)) 2
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
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))
Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t #f nil if special form
<if-true> <if-false>)
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
Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f)
Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1
Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3)
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
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)
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
Convert this Python code to Scheme:
square = lambda x: x**2
Convert this Python code to Scheme:
square = lambda x: x**2 (define square (lambda (x) (* x x)))
All functions in Scheme are lambda functions! Convert this Python code to Scheme:
square = lambda x: x**2 (define square (lambda (x) (* x x)))
>>> (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)))
remember linked lists?
1 4 9
first rest
Pair(1, Pair(4, Pair(9, nil)))
1 4 9
first rest
(cons 1 (cons 4 (cons 9 nil))) Pair(1, Pair(4, Pair(9, nil)))
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)
in scheme = can only be used for comparing numbers.
(= 2 2) (= ‘(1) ‘(1))
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)
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)
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)
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)
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)
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)
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)
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…
languages are well suited for functional programming.
Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/