Scheme Quick Review

Scheme is a programming language where every functional line of code (aka every line of code that actually does something) is structured as a list, in the following format:

(operator operand(s))

Every time you write a functional line of code, you have to structure it as a list (which means that it is enclosed within parentheses) where the very first thing in the list is an operator - This means that the very first thing after every open parenthesis must ALWAYS be an operator.

important scheme functions

Here are some of the Scheme functions that you will see throughout the Scheme unit. There are more, of course, but these are the ones you will see most of the time. Mouse over each function to see the Python parallel for each function.

(define name value)
Example:
(define x 3)
Sets name equal to value
Note: A define statement always returns the name of what it just defined
In Python:
x = 3
(define (function-name parameter)
    body)

Example:
(define (f x) (+ x 1))
Creates a function with name, parameter, and body
Note: A define statement always returns the name of what it just defined
In Python:
def f(x):
    return x + 1
((lambda (parameter1 parameter2)
    body) value1 value2)

Example:
((lambda (x y) (+ x y)) 3 4)
Creates a lambda function with parameters and body and calls it on values
In Python:
(lambda x, y: x + y)(3, 4)
(if condition true-result false-result)
Example:
(if (= 1 1) 1 0)
If the condition is true, returns true-result, else returns false-result
Note: The only false value in Scheme is #f, so 0 is a true value
In Python:
if 1 == 1:
    return 1
else:
    return 0
(or expression1 expression2)
Example:
(or (= x 1) (= y 2))
Evaluates each expression until it reaches one that evaluates to true or the last one and then returns its value
In Python:
x == 1 or y == 2
(and expression1 expression2)
Example:
(and (= x 1) (= y 2))
Evaluates each expression until it finds one that evaluates to false or the last one and then returns that value
In Python:
x == 1 and y == 2
(cond (condition1 result1) (condition2
    result2) (else result3))

Example:
(cond ((= x 1) 2) (( = x 2) 3) (else 4))
Evaluates each condition until it finds a true one or reaches else, then returns that result
In Python:
if x == 1:
    return 2
else if x == 2:
    return 3
else:
    return 4
(quote expression) or ‘expression
Example:
'hello
Returns expression exactly as is
Note: quote is a little different than print because it does no evaluation and just gives you exactly what you passed in, but this is the closest parallel I could come up with
In Python:
print(hello)
(eval expression)
Example:
(eval '3)
Evaluates and returns the given expression
In Python:
There's not really a parallel in Python that we teach in this class, so just remember that eval evalutes stuff
(= num1 num2)
Example:
(= 1 2)
Checks to see if num1 equals num2
Note: = can only be used with numbers
In Python:
1 == 2
(eq? x y)
Example:
(eq? 'hello 'goodbye)
Checks to see if x and y are equal in the same way that the is keyword does in Python
In Python:
"hello" is "goodbye"
(equal? x y)
Example:
(equal? 'hello 'goodbye)
Checks to see if x and y are equal in the same way that the == keyword does in Python
In Python:
"hello" == "goodbye"

scheme lists and list functions

Scheme lists are exactly the same as linked lists in Python.  They have a car, which is the same as first for a linked list, and a cdr, which is the same as rest for a linked list.  Similarly to linked lists, the cdr of a scheme list must always be another scheme list or nil (which is the empty list, just like Link.empty).

(null? lst) - Checks to see if list is null (returns a boolean value)

(cons first rest)
Example:
(cons 1 (cons 2 nil))
Creates a two element scheme list where first is the car and rest is the cdr
In Python:
Link(1, Link(2))
(list elem1 elem2 elem3)
Example:
(list 1 2 3)
Creates a Scheme list using the given elements
Note: You can have pass in any number of elements into list
In Python:
Link(1, Link(2, Link(3)))
(car lst)
Example:
(car (cons 1 (cons 2 nil))
Gives the car, or the first of the list
In Python:
lst = Link(1, Link(2))
lst.first
(cdr lst)
Example:
(cdr (cons 1 (cons 2 nil))
Gives the cdr, or the rest of the list
In Python:
lst = Link(1, Link(2))
lst.rest
(null? lst)
Example:
(null? (cons 1 (cons 2 nil))
Checks to see if the list is nil (which is the empty list) and returns a boolean value
In Python:
lst = Link(1, Link(2))
lst is Link.empty

If you’re feeling stressed don’t worry! You’ve seen pretty much all of these concepts before, so the only tricky part of Scheme is getting used to the new syntax. Also notice that I haven’t mentioned any functions that represent iteration (like while/for in Python). That’s because we don’t teach any form of iteration in Scheme. This means that you will be using recursion for pretty much any code writing question, so you will be a recursion boss at the end of this unit :)