Discussion 8

Scheme and Scheme Lists 🥶

Antonio Kam
anto [at] berkeley [dot] edu

All slides can be found on
teaching.rouxl.es

Slides by Antonio Kam (anto@)

Announcements

  • Ants Checkpoint due today (Project Party from 3PM - 5:30PM today in Warren 101B)
  • HW 5 due two days from now
  • The Scheme project gets released on Friday; will have a lab to help with getting started next Monday, as well as a lecture this Wednesday to help with understanding interpreters.
    • The project is a pretty funny mix of content from the end of the course with content from the start of the course
  • I'm giving a lecture on SQL on August 1st. Please pull up
    • I will be ending discussion earlier on that day so I can get to Dwinelle on time to get everything prepared
  • Reminder to finish the mid-semester feedback form
Slides by Antonio Kam (anto@)

Notes from last section

  • I like when you give us tips that helped you understand stuff because it often helps me also!
    • 🥺 that's very nice to hear
    • also extends to a lot of you during labs/discussions - if you have a way that you like seeing things that can help you, please also discuss with other people! something that you find helpful can usually really help other people
  • potato >> all
    • agreed
    • i had slivers yesterday cause they had the pizza with potatoes and corn 👌
Slides by Antonio Kam (anto@)

Notes from last section

  • If you could travel anywhere in the world (or galaxy), where would you go?
    • still haven't gone to nyc but i really want to go just cause i want to try good US public transport lol
    • ive been spoiled by asian city public transport
  • In the game Kirby Return to Dreamland, inhaling 2 enemies at once causes you to get a randomized ability, and outside of a specific stage, this is the only way to get the UFO Kirby power up, which has a laser attack and allows for infinite flight
    • what type of conditions do you need to be able to inhale 2 enemies at once? it seems pretty difficult to do, but ive also never played anything kirby related
Slides by Antonio Kam (anto@)

Temperature Check 🌡️

  • Linked Lists
  • Scheme
  • Scheme Lists
  • Would like to mention that I will be talking a lot this discussion at the start cause a lot of this is syntax
Slides by Antonio Kam (anto@)

What is Scheme

  • Scheme is another language that you need to learn 👀
  • It's a dialect of Lisp (List Processor)
  • Everything is done with recursion 🥳 🥲
    • No while/for loops
    • Good thing about this is that you get a lot of recursion/tree recursion practice with scheme
  • No mutation in scheme
  • IMO Scheme is a very good way to demonstrate that once you learn the logic for one programming language, learning a second one is way easier!
  • There are a lot of parentheses 😭
Slides by Antonio Kam (anto@)

Why learn scheme

  • ramble about something anto
Slides by Antonio Kam (anto@)

Primitives

  • Scheme has a set of atomic primitive expressions.
    • Similar to the primitives in Python
    • These expressions cannot be divided up further
scm> 1
1
scm> 2
2
scm> #t
True
scm> #f
False
Slides by Antonio Kam (anto@)

Booleans (Python)

Remember this table?

Falsey Truthy
False True
None Everything else
0
[], "", (), {}
Slides by Antonio Kam (anto@)

Booleans (Scheme)

Falsey Truthy
#f Everything else

🥳 This is something you need to remember 😭

Slides by Antonio Kam (anto@)

define

  • In scheme, everything that isn't a primitive is done with prefix notation
    • (<keyword> [<arguments> ...])
  • In scheme, we use the define keyword in order to bind values to symbols, which work the same way as variables.
    • This is also used to define functions - more on this later
    • This keyword returns the symbol:
Slides by Antonio Kam (anto@)
  • In scheme, everything that isn't a primitive is done with prefix notation
    • (<keyword> [<arguments> ...])
  • In scheme, we use the define keyword in order to bind values to symbols, which work the same way as variables.
    • This is also used to define functions - more on this later
    • This keyword returns the symbol:
>>> x = 3
scm> (define x 3)
x
Slides by Antonio Kam (anto@)

Intro WWSD (Maybe)

Slides by Antonio Kam (anto@)

Call Expressions

  • Call expressions apply a procedure to some arguments
    • (<operator> [<operands> ...])
  • Exactly the same process as Python
  • Evaluate the operator (make sure it's a procedure/function)
  • Evaluate each of the operands (from left to right)
  • Apply the operands to the operator
Slides by Antonio Kam (anto@)

Call Expression WWSD (Maybe)

Slides by Antonio Kam (anto@)

Call Expressions

>>> add(1, 2)
3
scm> (+ 1 2) 
3
  • Important to note that all the operands are evaluated!
Slides by Antonio Kam (anto@)

Special Forms

  • They still look like call expressions (syntax-wise), but instead of evaluating all the operators, there are certain rules for evaluation.
Slides by Antonio Kam (anto@)

Special Forms (if)

  • (if <predicate> <if-true> [<if-false>])
  • <predicate> and <if-true> are required, <if-false> is optional
  • Rule for evaluation:
    • Evaluate <predicate>
    • If <predicate> is truthy (don't forget what Scheme does!), evaluate <if-true>
    • Else evaluate <if-false> (if it exists)
  • This means that not all of its operands will be evaluated!
Slides by Antonio Kam (anto@)

Special Forms (if)

scm> (if (> 4 3) 3 2)
3
scm> (if 0 3 2)
3
scm> (if #f 3 2)
2
scm> (if (= 3 2) (/ 1 0) 3)
3
scm> (if (= 3 3) (/ 1 0) 3)
Error
Slides by Antonio Kam (anto@)

Special Forms (cond)

  • This is the better one 😄 (I default to this even when it's not necessary)
  • It's the way to handle elif statements in Scheme
(cond 
    (<pred1> <if-pred1>) ; usually <pred> is something like (= x 2)
    (<pred2> <if-pred2>) 
    ... 
    (<predn> <if-predn>) 
    [(else <else-expression>)]
)
Slides by Antonio Kam (anto@)

Special Forms (cond) Example

(cond 
    ((= x 2) 3) 
    ((= x 3) (+ x 1))  
    (else 'hello)
)
Slides by Antonio Kam (anto@)

Special Forms (Boolean Operators)

  • and, or, not

    • (and 1 2 3)3
    • (or 1 2 3)1
    • (not 0)#f
  • Equivalence

    • = - used for numbers
    • eq? - is in Python
    • equal? - == in Python
Slides by Antonio Kam (anto@)

Defining Functions

  • All functions are lambda functions in scheme.
(lambda ([<params> ...]) <body>)

scm> (lambda (x) (+ x 2))
(lambda (x) (+ x 2))
scm> (define f (lambda (x) (+ x 2)))
f
scm> f
(lambda (x) (+ x 2))
Slides by Antonio Kam (anto@)

Defining Functions

  • There is a bit of a shorthand to write functions:
(define (<name> [<params> ...]) <body>) 

scm> (define (f x) (+ x 2))
f
scm> f
(lambda (x) (+ x 2))
Slides by Antonio Kam (anto@)

Executing Functions

  • By default, functions will return the last expression in the body:
(define (f x) 
    (+ 1 2) ; doesn't get returned in the terminal
    (+ 2 x) ; this implicitly gets returned
)
def f(x):
    1 + 2
    return 2 + x
Slides by Antonio Kam (anto@)

Worksheet!

Slides by Antonio Kam (anto@)

Pairs and Lists

Slides by Antonio Kam (anto@)

What are Scheme Lists?

  • All Scheme lists are very similar to the Python linked lists that we've been dealing with.
  • Python:
    • lnk.first - gets the first element
    • lnk.rest - gets the rest of your linked list
  • Scheme:
    • (car lnk) - gets the first element
    • (cdr lnk) - gets the rest of your scheme list
  • Weird names!
Slides by Antonio Kam (anto@)

Creating Scheme Lists

>>> Link(1, Link(2, Link(3)))
Link(1, Link(2, Link(3)))
scm> (cons 1 (cons 2 (cons 3 nil)))
(1 2 3)
scm> (list 1 2 3)
(1 2 3)
scm> '(1 2 3)
(1 2 3)
Slides by Antonio Kam (anto@)

Worksheet!

Slides by Antonio Kam (anto@)

Results from last section (links.rouxl.es/disc)

  • baked (4)
    • "like the baked potato in minecraft"
  • I would like to be cooked whole since I don't want to get cut up
  • fried (2)
    • I would like to be fried because it would be like a jacuzzi, but I can't decide if I want to be made into french fries, wedges, hasselback, or pave because they all involve getting cut up :(
  • mashed (2)
  • scalloped
  • raw
  • roasted
  • made into chips

Mental Health Resources

  • CAPS:
    • If you need to talk to a professional, please call CAPS at 510-642-9494.
  • After Hours Assistance
    • For any assistance after hours, details on what to do can be found at this link
Slides by Antonio Kam (anto@)

Anonymous Feedback Form
links.rouxl.es/feedback

Thanks for coming! 🥳

Please give me feedback on what to improve!

Slides by Antonio Kam (anto@)