Scheme Quick Review

Scheme is a functional programming language, where every functional line of code (basically any line that does anything) is structured as a list and placed in parentheses. The first thing after the open parenthesis MUST ALWAYS be a function/operation.

Some Important Functions

(cons x y) - Creates a two element list with x as the first element and y as the second element

(car lst) - Gives the car, or the first element of lst

(cdr lst) - Gives the cdr, or the second element of lst

(list x y z) - Creates a well-formed list out of x, y, and z

(if condition true-result false-result) - If the condition is true, returns true-result, otherwise returns false result

(cond (condition1 result1) (condition2 result2) (else result3)) - Evaluates each condition until it finds a true one or reaches else, then returns that result

(or expression1 expression2 expression3) - Evaluates each expression until it reaches one that evaluates true or the last one and returns its value

(and expression1 expression2 expression3) - Evaluates each expression until it finds one that evaluates false or the last one and returns that value

(define name value) - Sets name equal to value (kind of like an assignment statement)

(define (function-name parameter) body) - Creates a function with name, parameter, and body (kind of like a def statement)

((lambda (parameter1 parameter2) body) value1 value2) - Creates a lambda function with parameters and body and calls it on values

(= num1 num2) - Checks to see if num1 and num2 are equal (can only be used for numbers)

(eq? x y) - Checks to see if x and y are equal in the same way that the is keyword does in python (returns a boolean value)

(equal? x y) - Checks to see if x and y are equal in the same way that the == does in python (returns a boolean value)

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

(quote expression) or ‘expression - Returns expression exactly as is (sort of like a print)

(eval expression) - Evaluates expression and returns the evaluated expression

There are more of course, but these are the ones you will see most of the time.

extra Topic: malformed lists

Note: this is not a required topic, this is just something that I learned when I was in the class, but is no longer taught in 61A

Up until this point, we’ve only talked about what are called well-formed lists, or the equivalent of linked lists in python. However, in scheme, we can have other types of lists, specifically malformed lists. Malformed lists also have a car and a cdr, but the cdr is NOT a well-formed list or nil (it will usually be a number/symbol instead). In scheme code, we represent a malformed list using a dot in between the car and cdr:

(cons 1 2) => (1 . 2)

We can see that the car of this list is 1, and the cdr of this list is 2 (which is not a well-formed list or nil, so we know that this list is not well-formed)

Malformed lists can be visually represented in the same way that python lists are represented.

Using the same list from above, we can visualize it like so:

Screen Shot 2019-05-30 at 6.11.14 PM.png