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.
Example:
(define x 3)
Sets name equal to value
Note: A define statement always returns the name of what it just defined
x = 3
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
def f(x):
return x + 1
body) value1 value2)
Example:
((lambda (x y) (+ x y)) 3 4)
Creates a lambda function with parameters and body and calls it on values
(lambda x, y: x + y)(3, 4)
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
if 1 == 1:
return 1
else:
return 0
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
x == 1 or y == 2
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
x == 1 and y == 2
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
if x == 1:
return 2
else if x == 2:
return 3
else:
return 4
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
print(hello)
Example:
(eval '3)
Evaluates and returns the given expression
There's not really a parallel in Python that we teach in this class, so just remember that eval evalutes stuff
Example:
(= 1 2)
Checks to see if num1 equals num2
Note: = can only be used with numbers
1 == 2
Example:
(eq? 'hello 'goodbye)
Checks to see if x and y are equal in the same way that the is keyword does in Python
"hello" is "goodbye"
Example:
(equal? 'hello 'goodbye)
Checks to see if x and y are equal in the same way that the == keyword does 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)
Example:
(cons 1 (cons 2 nil))
Creates a two element scheme list where first is the car and rest is the cdr
Link(1, Link(2))
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
Link(1, Link(2, Link(3)))
Example:
(car (cons 1 (cons 2 nil))
Gives the car, or the first of the list
lst = Link(1, Link(2))
lst.first
Example:
(cdr (cons 1 (cons 2 nil))
Gives the cdr, or the rest of the list
lst = Link(1, Link(2))
lst.rest
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
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 :)