![Page 1: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/1.jpg)
Marvin Zhang 07/25/2016
Lecture 19: Scheme I
![Page 3: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/3.jpg)
Roadmap
Introduction
Functions
Data
Mutability
Objects
Interpretation
Paradigms
Applications
![Page 4: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/4.jpg)
Roadmap
• This week (Interpretation), the goals are:
Introduction
Functions
Data
Mutability
Objects
Interpretation
Paradigms
Applications
![Page 5: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/5.jpg)
Roadmap
• This week (Interpretation), the goals are:• To learn a new language, Scheme,
in two days!
Introduction
Functions
Data
Mutability
Objects
Interpretation
Paradigms
Applications
![Page 6: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/6.jpg)
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
![Page 7: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/7.jpg)
Scheme
![Page 8: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/8.jpg)
Scheme
• Scheme is a dialect of Lisp, the second-oldest language still used today
![Page 9: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/9.jpg)
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
![Page 10: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/10.jpg)
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
![Page 11: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/11.jpg)
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
![Page 12: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/12.jpg)
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?
![Page 13: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/13.jpg)
Scheme Fundamentals
![Page 14: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/14.jpg)
Scheme Fundamentals
• Scheme primitives include numbers, Booleans, and symbols
![Page 15: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/15.jpg)
Scheme Fundamentals
• Scheme primitives include numbers, Booleans, and symbols• More on symbols later (for now, they’re like variables)
![Page 16: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/16.jpg)
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
![Page 17: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/17.jpg)
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
or more operands, all surrounded by parentheses
![Page 18: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/18.jpg)
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
or more operands, all surrounded by parentheses
(demo)
![Page 19: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/19.jpg)
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
or more operands, all surrounded by parentheses
scm> (quotient (+ 8 7) 5)3
(demo)
![Page 20: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/20.jpg)
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
or more operands, all surrounded by parentheses
scm> (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))57
scm> (quotient (+ 8 7) 5)3
(demo)
![Page 21: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/21.jpg)
Assignment, Symbols, Functions, and Conditionals
Special Forms
![Page 22: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/22.jpg)
Assignment Statements
![Page 23: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/23.jpg)
Assignment Statements
• Special forms in Scheme have special orders of evaluation
![Page 24: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/24.jpg)
Assignment Statements
• Special forms in Scheme have special orders of evaluation
• We can bind symbols to values using define
![Page 25: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/25.jpg)
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
![Page 26: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/26.jpg)
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)
![Page 27: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/27.jpg)
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
![Page 28: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/28.jpg)
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)ascm> a
![Page 29: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/29.jpg)
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)ascm> a5
![Page 30: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/30.jpg)
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)ascm> a5
scm> (define b (+ a 4))
![Page 31: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/31.jpg)
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)ascm> a5
scm> (define b (+ a 4))b
![Page 32: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/32.jpg)
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)ascm> a5
scm> (define b (+ a 4))bscm> b
![Page 33: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/33.jpg)
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)ascm> a5
scm> (define b (+ a 4))bscm> b9
![Page 34: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/34.jpg)
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)ascm> a5
scm> (define b (+ a 4))bscm> b9
Expressions
![Page 35: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/35.jpg)
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)ascm> a5
scm> (define b (+ a 4))bscm> b9
Expressions
![Page 36: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/36.jpg)
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)ascm> a5
scm> (define b (+ a 4))bscm> b9
Expressions
![Page 37: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/37.jpg)
Symbols and quote
![Page 38: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/38.jpg)
Symbols and quote
• Symbols are like variables, they can be bound to values
![Page 39: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/39.jpg)
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
![Page 40: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/40.jpg)
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
![Page 41: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/41.jpg)
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
![Page 42: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/42.jpg)
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)
![Page 43: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/43.jpg)
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
![Page 44: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/44.jpg)
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)ascm> a
![Page 45: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/45.jpg)
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)ascm> a5
![Page 46: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/46.jpg)
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)ascm> a5
scm> (quote a)
![Page 47: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/47.jpg)
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)ascm> a5
scm> (quote a)a
![Page 48: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/48.jpg)
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)ascm> a5
scm> (quote a)ascm> 'a ; shorthand for (quote a)
![Page 49: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/49.jpg)
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)ascm> a5
scm> (quote a)ascm> 'a ; shorthand for (quote a)a
![Page 50: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/50.jpg)
Assignment Expressions
![Page 51: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/51.jpg)
Assignment Expressions
• define expressions evaluate to the symbol that was bound, not the value the symbol was bound to
![Page 52: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/52.jpg)
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
![Page 53: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/53.jpg)
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)
![Page 54: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/54.jpg)
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)ascm> (define b a)bscm> b5
scm> (define c (define a 3))cscm> a3scm> ca
(demo)
![Page 55: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/55.jpg)
Lambda Expressions
![Page 56: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/56.jpg)
Lambda Expressions
• lambda expressions evaluate to anonymous procedures
![Page 57: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/57.jpg)
Lambda Expressions
• lambda expressions evaluate to anonymous procedures
• (lambda (<parameters>) <body>) creates a procedure as the side effect, and evaluates to the procedure itself
![Page 58: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/58.jpg)
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)
![Page 59: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/59.jpg)
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)
operator
![Page 60: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/60.jpg)
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)
operator operand
![Page 61: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/61.jpg)
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)))
operator operand
![Page 62: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/62.jpg)
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
operator operand
![Page 63: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/63.jpg)
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!
operator operand
![Page 64: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/64.jpg)
Conditionals and Booleans
![Page 65: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/65.jpg)
Conditionals and Booleans
• Conditional expressions come in two types:
![Page 66: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/66.jpg)
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>
![Page 67: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/67.jpg)
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 Pythonif-elif-else statements using the cond expression
![Page 68: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/68.jpg)
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 Pythonif-elif-else statements using the cond expression
(demo)
![Page 69: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/69.jpg)
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 Pythonif-elif-else statements using the cond expression
scm> (cond ((= 3 4) 4) ((= 3 3) 0) (else 'hi))0
(demo)
![Page 70: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/70.jpg)
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 Pythonif-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))0
(demo)
![Page 71: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/71.jpg)
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 Pythonif-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))0
(demo)
![Page 72: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/72.jpg)
Scheme data structures
Pairs and Lists
![Page 73: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/73.jpg)
Pairs and Lists
![Page 74: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/74.jpg)
Pairs and Lists
• Disclaimer: programmers in the 1950s used confusing terms
![Page 75: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/75.jpg)
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
![Page 76: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/76.jpg)
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
![Page 77: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/77.jpg)
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))
![Page 78: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/78.jpg)
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
![Page 79: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/79.jpg)
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))xscm> x
![Page 80: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/80.jpg)
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))xscm> x(1 . 3)
![Page 81: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/81.jpg)
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))xscm> x(1 . 3)scm> (car x)
![Page 82: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/82.jpg)
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))xscm> x(1 . 3)scm> (car x)1
![Page 83: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/83.jpg)
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))xscm> x(1 . 3)scm> (car x)1scm> (cdr x)
![Page 84: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/84.jpg)
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))xscm> x(1 . 3)scm> (car x)1scm> (cdr x)3
![Page 85: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/85.jpg)
Pairs and Lists
![Page 86: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/86.jpg)
Pairs and Lists
• The only type of sequence in Scheme is the linked list, which we can create using just pairs!
![Page 87: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/87.jpg)
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
![Page 88: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/88.jpg)
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
![Page 89: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/89.jpg)
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)
![Page 90: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/90.jpg)
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))))xscm> x ; no dots displayed for well-formed lists(1 2 3)scm> (car x)1scm> (cdr x)(2 3)
scm> (list 1 2 3) ; shorthand(1 2 3)scm> '(1 2 3) ; shortest-hand(1 2 3)
![Page 91: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/91.jpg)
Coding Practice
![Page 92: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/92.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-element procedure and lst is a (linked) list
![Page 93: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/93.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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
![Page 94: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/94.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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
![Page 95: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/95.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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
![Page 96: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/96.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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!
![Page 97: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/97.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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)
![Page 98: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/98.jpg)
Coding Practice
• Let’s implement a procedure (map fn lst), where fn is a one-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)))))
![Page 99: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/99.jpg)
More Coding Practice
![Page 100: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/100.jpg)
More Coding Practice
• We can create a tree abstraction just like in Python:
![Page 101: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/101.jpg)
More Coding Practice
• We can create a tree abstraction just like in Python:(define (tree entry children)
![Page 102: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/102.jpg)
More Coding Practice
• We can create a tree abstraction just like in Python:(define (tree entry children) (cons entry children))
![Page 103: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/103.jpg)
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))
![Page 104: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/104.jpg)
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))
![Page 105: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/105.jpg)
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)
![Page 106: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/106.jpg)
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)))
![Page 107: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/107.jpg)
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)
![Page 108: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/108.jpg)
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)))))
![Page 109: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/109.jpg)
Summary
![Page 110: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/110.jpg)
Summary
• We learned a new language today! Being able to quickly pick up new languages is important for good programmers
![Page 111: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/111.jpg)
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
![Page 112: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/112.jpg)
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
![Page 113: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/113.jpg)
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
![Page 114: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/114.jpg)
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
![Page 115: Lecture 19: Scheme Ics61a/su16/assets/slides/19-Scheme… · Scheme • Scheme is a dialect of Lisp, the second-oldest language still used today • “If you don't know Lisp, you](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f5d307e708231d443cad6/html5/thumbnails/115.jpg)
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!