class: list # welcome to discussion 9! Announcements (starting berkeley time!): - Cats Composition Revisions due Thursday 4/2 @ 11:59pm. - Lab 9 due Thursday 4/2 @ 11:59pm. - Homework 6 due Friday 4/3 @ 11:59pm. - Office hours: make an appointment or join the drop-in queue during drop-in online office hours times to ask questions. - Lecture Q&A session on Zoom MWF 7:30pm-8:15pm. - "Optional" lecture content will not appear in lab, discussion, homework, projects, or exams. - Feedback **esp. on this online format!!**: **links.cs61a.org/addison-feedback** - We have two AIs today! - Vanshaj (VS) - Aditya - They will be bouncing around zoom rooms to help out! --- class: list # schedule Schedule: - Intro to scheme, Primitives, define, and call expressions - WWSD: Define/Call Expressions - special forms - WWSD: Special forms - Lambdas and Defining Functions - Factorial - Pairs and Lists - my-append - Quiz ? --- class: title # scheme The second programming language you're taught in 61A. A bit of history: 61A used to be taught in scheme! --- class: title # *atomic* primitive expressions Numbers, booleans, strings, empty list (`nil`), and symbols (more on this later). ```scheme scm> 123 ; number 123 scm> #t ;boolean -- false is #f True scm> "hi" ;string "hi" scm> nil ;empty list () ``` --- class: title # define statements These are *special forms*, meaning there are special evaluation rules. ```scheme ; assignment: (define
) ; function (or procedure): (define (
)
) ``` When invoked, the `define` special form will return the _symbol_ of its name. --- class: title # define - assignment ```scheme scm> (define ais "Aditya and Vanshaj") ais scm> ais "Aditya and Vanshaj" ``` compare to Python: ```python >>> ais = "Aditya and Vanshaj" >>> ais 'Aditya and Vanshaj' ``` --- class: title # define - function ```scheme scm> (define (addone x) (+ x 1)) addone scm> (addone 1) 2 ``` compare to Python: ```python >>> addone = lambda x: x + 1 >>> addone(1) 2 ``` --- class: title # call expressions ```scheme (
...) ``` -- you just saw this! -- let's break it down: ```scheme scm> (define (addone x) (+ x 1)) addone scm> (addone 1) 2 ``` -- ```scheme scm> (- 1 1) ; 1 - 1 0 scm> (/ 8 4 2) ; 8 / 4 / 2 1 ``` --- class: title # WWSD --- class: title # special forms - `if` - boolean operators (`and`, `or`, `not`) --- class: title # special forms: if `(if
[if-false])` ```scheme scm> (if #t 1) 1 scm> (if 0 "hello" "bye") ``` --- class: title # special forms: if `(if
[if-false])` ```scheme scm> (if #t 1) 1 scm> (if 0 "hello" "bye") "hello" ``` **`#f` is the only false-y value!** --- class: title # special forms: if `(if
[if-false])` ```scheme scm> (if #t 1) 1 scm> (if 0 "hello" "bye") "hello" scm> (if #f "hello" "bye") ``` --- class: title # special forms: if `(if
[if-false])` ```scheme scm> (if #t 1) 1 scm> (if 0 "hello" "bye") "hello" scm> (if #f "hello" "bye") "bye" ``` --- class: title # (s.f.) - boolean operators - `and` takes in any amount of operands until it hits the first false-y value. Returns value of last expression (defaults to `#t`) - `or` takes in any amount of operands until it hits the first truth-y value. Returns value of last expression (defaults to `#f`) - `not` takes in one operand and returns its opposite truthiness. (`not` is a regular procedure and not a special form!) - *RECALL: Only `#f` is false-y! Everything else is **truthy**!* ```scheme scm> (and 25 32) 32 scm> (or 1 (/1 0)) ; short-circuits! 1 scm> (not (odd? 10)) #t ``` --- class: title # WWSD --- class: title # lambdas and functions We saw this earlier: ```scheme (define (addone x) (+ x 1)) ``` but it was really shorthand for this: ```scheme (define addone (lambda (x) (+ x 1))) ``` Form: `(lambda (
...) )` --- class: title # factorial breakout rooms! -- ```scheme (define (factorial x) (if (< x 2) 1 (* x (factorial (- x 1))))) ``` --- class: title # pairs and lists Remember this? ```python Link(first, next) ``` where next was either `nil` or another `Link`? -- In scheme, all lists are *linked lists*! --- class: title # pairs and lists - `(cons first rest)` constructs a list. This is akin to `Link(first, rest)` - `(car lst)` gets the first item of a list. This is akin to `lnk.first` - `(cdr lst)` get the rest of the list. This is akin to `lnk.rest` - `nil` represents the empty list. This is akin to `Link.empty` -- ```scheme scm> nil () ; remind you of Link.empty? scm> ; Python: lst = Link(1, Link(2, Link(3, Link.empty))) scm> (define lst (cons 1 (cons 2 (cons 3 nil)))) lst scm> lst (1 2 3) scm> (car lst) ; Python: lst.first 1 scm> (cdr lst) ; Python: lst.rest (2 3) scm> (car (cdr lst)) ; Python: lst.rest.first 2 ``` --- class: title # lists: etc other ways to make lists: ```scheme scm> (list 1 2 3) (1 2 3) scm> (quote (1 2 3)) ; quote means "use this literally" (1 2 3) scm> '(1 2 3) ; equivalent (1 2 3) ``` --- class: title # =, eq?, equal? - = for **numbers**. - `eq?` is Scheme analogue of `==` in Python for nonlists. Otherwise, **it is the analogue of `is`**. - `equal?` is Scheme analogue of `==` in Python for everything. -- ```scheme scm> (= '(1 2 3) '(1 2 3)) ; remember: only for numbers Error: operand 0 ((1 2 3)) is not a number scm> (eq? '(1 2 3) '(1 2 3)) ; remember: Python `is` #f scm> (equal? '(1 2 3) '(1 2 3)) ; remember: Python `==` #t ``` --- class: title # my-append breakout rooms!