You are on page 1of 2

CS 61A Final Exam Study Guide – Page 1

(de ne size 5) ; => size


(* 2 size) ; => 10
Exceptions are raised with a raise statement. (if (> size 0) size (- size)) ; => 5
raise <expr> (cond ((> size 0) size) ((= size 0) 0) (else (- size))) ; => 5
<expr> must evaluate to a subclass of BaseException or an instance of one. ((lambda (x y) (+ x y size)) size (+ 1 2)) ; => 13
(let ((a size) (b (+ 1 2))) (* 2 a b)) ; => 30
try: >>> try:
<try suite>
(map (lambda (x) (+ x size)) (quote (2 3 4))) ; => (7 8 9
x = 1/0
except <exception class> as <name>: except ZeroDivisionError as e: ( lter odd? (quote (2 3 4))) ; => (3)
<except suite> print('handling a', type(e)) (list (cons 1 nil) size 'size) ; => ((1) 5 size)
x = 0 (list (equal? 1 2) (null? nil) (= 3 4) (eq? 5 5)) ; => (#f #t #f #t)
The <try suite> is executed first.
(list (or #f #t) (or) (or 1 2)) ; => (#t #f 1)
If, during the course of executing the handling a <class 'ZeroDivisionError'>
>>> x (list (and #f #t) (and) (and 1 2)) ; => (#f #t 2
<try suite>, an exception is raised
that is not handled otherwise, and 0 (append '(1 2) '(3 4)) ; => (1 2 3 4)
(not (> 1 2)) ; => #t
If the class of the exception inherits from <exception class>, then
(begin (de ne x (+ size 1)) (* x 2)) ; => 12
The <except suite> is executed, with <name> bound to the exception. `(+ size (- ,size) ,(* 3 4)) ; => (+ size (- 5) 12)

The built-in Scheme list data structure can represent combinations A table has columns and rows
scm> (list 'quotient 10 2) scm> (eval (list 'quotient 10 2)) Latitude Longitude Name A column
(quotient 10 2) 5 has a
38 122 Berkeley name and
42 71 Cambridge a type
There are two ways to quote an expression
45 93 Minneapolis
Quote: '(a b) => (a b)
Quasiquote: `(a b) => (a b) A row has a value for each column

They are different because parts of a quasiquoted expression can be SELECT [expression] AS [name], [expression] AS [name], ... ;
unquoted with ,
SELECT [columns] FROM [table] WHERE [condition] ORDER BY [order];
(define b 4)
CREATE TABLE parents AS
Quote: '(a ,(+ b 1)) => (a (unquote (+ b 1))
SELECT "abraham" AS parent, "barack" AS child UNION
Quasiquote: `(a ,(+ b 1)) => (a 5)
SELECT "abraham" , "clinton" UNION E
SELECT "delano" , "herbert" UNION
Quasiquotation is particularly convenient for generating Scheme
SELECT "fillmore" , "abraham" UNION
expressions:
SELECT "fillmore" , "delano" UNION
SELECT "fillmore" , "grover" UNION F
(define (make-add-procedure n) `(lambda (d) (+ d ,n)))
SELECT "eisenhower" , "fillmore";
(make-add-procedure 2) => (lambda (d) (+ d 2))
CREATE TABLE dogs AS
; Sum the squares of even numbers less than 10, starting with 2
SELECT "abraham" AS name, "long" AS fur UNION
; RESULT: 2 * 2 + 4 * 4 + 6 * 6 + 8 * 8 = 12
SELECT "barack" , "short" UNION
A D G
(begin
SELECT "clinton" , "long" UNION
(define (f x total SELECT "delano" , "long" UNION
(if (< x 10 SELECT "eisenhower" , "short" UNION B C H
(f (+ x 2) (+ total (* x x)) SELECT "fillmore" , "curly" UNION
total) SELECT "grover" , "short" UNION
(f 2 0) SELECT "herbert" , "curly"; First Second
barack clinton
; Sum the numbers whose squares are less than 50, starting with 1 SELECT a.child AS first, b.child AS second
abraham delano
; RESULT: 1 + 2 + 3 + 4 + 5 + 6 + 7 = 2 FROM parents AS a, parents AS b
(begin WHERE a.parent = b.parent AND a.child < b.child; abraham grover
(define (f x total delano grover
(if (< (* x x) 50 The number of groups is the number of unique values of an expression
(f (+ x 1) (+ total x)
A having clause filters the set of groups that are aggregated
total)
select weight/legs, count(*) from animals
(f 1 0)
group by weight/legs kind legs weight
(define (sum-while starting-x while-condition add-to-total update-x
having count(*)>1;
; (eval (sum-while 2 '(< x 10) '(* x x) '(+ x 2))) => 120 weight/ weight/legs=5 dog 4 20
; (eval (sum-while 1 '(< (* x x) 50) 'x '(+ x 1))) => 28 count(*)
legs
`(begin weight/legs=2 cat 4 10
(define (f x total 5 2
(if ,while-condition weight/legs=2 ferret 4 10
(f ,update-x (+ total ,add-to-total)
total) 2 2 parrot
weight/legs=3 2 6
(f ,starting-x 0)))
weight/legs=5 penguin 2 10
A procedure call that has not yet returned is active. Some procedure
calls are tail calls. A Scheme interpreter should support an unbounded weight/legs=6000 t-rex 2 12000
number of active tail calls.
Matches any One or
A tail call is a call expression in a tail context, which are: . .a. cal, ha!, (a)
character + more aw+ awwwww
• The last body expression in a lambda expression copies
• Expressions 2 & 3 (consequent & alternative) in a tail context if Matches letters, cal, dad,
\w \wa\w Zero or by, buy,
• All final sub-expressions in a tail context cond numbers or _ 3am
* more b[a-z]*y buoy,
• The last sub-expression in a tail context and, or, begin, or let
\d Matches a digit \d\d 61, 00 copies berkeley
(define (factorial n k) (define (length s)
Matches a Zero or
\s \d\s\d 12 ? :[-o]?\) :) :o) :-)
(if (= n 0) k (if (null? s) 0 whitespace one copy
(factorial (- n 1) (+ 1 (length (cdr s))))) character bad, bed,
Encloses a list of yaay,
[...] b[aeiou]d {min, A range
(* k n)))) options or rangesbid, bod, ya{2,4}y yaaay,
bud max} of copies
Not a tail call yaaaay
a word followed by . (e.g., berkeley.)
(define (length-tail s)
a letter or number (or _) any letter (upper or lower case)
(define (length-iter s n) Recursive call is a tail call
(if (null? s) n word
(length-iter (cdr s) (+ 1 n)) ) )
(length-iter s 0) ) \w+@(\w+\.)+[A-Za-z]{3}
exactly three letters
The way in which names are looked up in Scheme and Python is one or more letters/numbers
called lexical scope (or static scope).
one or more parts of a domain name ending in .
Lexical scope: The parent of a frame is the environment in
which a procedure was defined. (lambda ...) The | character matches either of two sequences
Dynamic scope: The parent of a frame is the environment in (Fall|Spring) 20(\d\d) matches either Fall 2021 or Spring 2021
which a procedure was called. (mu ...) A whole group can be repeated multiple times
> (define f (mu (x) (+ x y))) l(ol)+ matches lol and lolol and lololol but not lolo
> (define g (lambda (x y) (f (+ x x))))
> (g 3 7) The ^ and $ anchors correspond to the start and end of the full string
13 The \b anchor corresponds to the beginning or end of a word
fi

fi

fi
)

Expression Trees
A basic interpreter has two parts: a parser and an evaluator

A basic interpreter has two parts: a parser and


scheme_reader.py an evaluator
scalc.py
CS 61A Final Exam Study Guide – Page 2 A basic interpreter has two parts: a parser and an evaluator.
scheme_reader.py scalc.py
Scheme programs consist of expressions, which can be: lines Parser expression Evaluator value
• Primitive expressions: 2, 3.3, true, +, quotient, ...
lines Parser expression Evaluator value
• Combinations: (quotient 10 2), (not true), ...
Numbers are self-evaluating; symbols are bound to values. '(+ 2 2)' Pair('+', Pair(2, Pair(2, nil))) 4
Call expressions have an operator and 0 or more operands.
'(+ 2 2)' Pair('+', Pair(2, Pair(2, nil))) 4
A combination that is not a call expression is a special form:
'(* (+ 1'
• If expression: (if <predicate> <consequent> <alternative>) Pair('*', Pair(Pair('+', ...)))
''(* (+ (-
1' 23)' 4
• Binding names: (define <name> <expression>) printed as
Pair('*', Pair(Pair('+', ...)))
• New procedures: (define (<name> <formal parameters>) <body>) '' (*
(- 423)'
5.6))' 4
(* (+ 1 (- 23) (*
printed as 4 5.6)) 10)
'' 10)'(* 4 5.6))'
(* (+ 1 (- 23) (* 4 5.6)) 10)
> (define pi 3.14) > (define (abs x) ' 10)'
> (* pi 2) (if (< x 0)
6.28 (- x) Lines forming
A number or a Pair with an
aLines
Scheme A number
x)) forming
expression
operator
A as aits
number or first
Pair with element
an
a Scheme A number
> (abs -3) expression
operator as its first element
3 A Scheme list is written as elements in parentheses:
6

Lambda expressions evaluate to anonymous procedures. 6

(<element0> <element1> ... <elementn>) A Scheme list


(lambda (<formal-parameters>) <body>)
Two equivalent expressions:
(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))
λ Each <element> can be a combination or atom (primitive).
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
The task of parsing a language involves coercing a string
An operator can be a combination too:
representation of an expression to the expression itself.
((lambda (x y z) (+ x y (square z))) 1 2 3)
Parsers must validate that expressions are well-formed.
A Parser takes a sequence of lines and returns an expression.
In the late 1950s, computer scientists used confusing names.
• cons: Two-argument procedure that creates a pair Lexical Syntactic
• car: Procedure that returns the first element of a pair Lines Tokens Expression
analysis analysis
• cdr: Procedure that returns the second element of a pair
• nil: The empty list
They also used a non-obvious notation for linked lists. '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...))
• A (linked) Scheme list is a pair in which the second element is ' (- 23)' '(', '-', 23, ')' printed as
nil or a Scheme list. ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6))
• Scheme lists are written as space-separated combinations.
• A dotted list has an arbitrary value for the second element of the
last pair. Dotted lists may not be well-formed lists. • Iterative process • Tree-recursive process
> (define x (cons 1 nil)) • Checks for malformed tokens • Balances parentheses
> x • Determines types of tokens • Returns tree structure
(1) • Processes one line at a time • Processes multiple lines
> (car x)
1 Syntactic analysis identifies the hierarchical structure of an
> (cdr x) expression, which may be nested.
()
> (cons 1 (cons 2 (cons 3 (cons 4 nil)))) Each call to scheme_read consumes the input tokens for exactly
(1 2 3 4) one expression.
Symbols normally refer to values; how do we refer to symbols? Base case: symbols and numbers
> (define a 1) Recursive call: scheme_read sub-expressions and combine them
> (define b 2) No sign of “a” and “b” in
> (list a b) The structure
the resulting value Base cases: Eval
(1 2) of the Scheme
• Primitive values (numbers)
interpreter
Quotation is used to refer to symbols directly in Lisp. • Look up values bound to symbols
> (list 'a 'b) Recursive calls: Creates a new
(a b) • Eval(operator, operands) of call expressions environment each
Symbols are now values time a user-
> (list 'a b) • Apply(procedure, arguments) defined procedure
(a 2) • Eval(sub-expressions) of special forms is applied
Quotation can also be applied to combinations to form lists.
> (car '(a b c))
a Requires an Apply
> (cdr '(a b c)) Base cases:
environment • Built-in primitive procedures
(b c) for name
lookup Recursive calls:
(car (cons 1 nil)) -> 1 • Eval(body) of user-defined procedures
(cdr (cons 1 nil)) -> ()
(cdr (cons 1 (cons 2 nil))) -> (2)
To apply a user-defined procedure, create a new frame in which
class Pair formal parameters are bound to argument values, whose parent
"""A pair has two instance attributes: is the env of the procedure, then evaluate the body of the
first and rest. procedure in the environment that starts with this new frame.
(define (f s) (if (null? s) '(3) (cons (car s) (f (cdr s)))))
rest must be a Pair or nil.
""" (f (list 1 2))
def __init__(self, first, rest)
self.first = firs g: Global frame
self.rest = rest f LambdaProcedure instance [parent=g]
Pair Pair
>>> s = Pair(1, Pair(2, Pair(3, nil))
>>> first rest first rest first rest [parent=g] s 1 2 nil
Pair(1, Pair(2, Pair(3, nil)) 1 2 3 nil
>>> print(s
[parent=g] s
(1 2 3)

[parent=g] s
The Calculator language has primitive expressions and call expressions

Calculator Expression Expression Tree A special symbol ?start corresponds to a complete expression.
Symbols in all caps are called terminals:
(* 3 • Can only contain /regular expressions/, "text", and other TERMINALS
(+ 4 5) • No recursion is allowed within terminals
(* 6 7 8)) * 3
?start: numbers
+ 4 5 * 6 7 8 numbers: INTEGER | numbers "," INTEGER
Representation as Pairs INTEGER: "0" | /-?[1-9]\d*/
• (item item ..) - Group items
first rest first rest first rest first rest
* 3 nil
• [item item ..] - Maybe. Same as (item item ..)?
• item? - Zero or one instances of item (”maybe”)
first rest first rest first rest first rest
• item* - Zero or more instances of item
* 6 7 8 nil • item+ - One or more instances of item
• item ~ n - Exactly n instances of item
first rest first rest first rest • item ~ n..m - Between n to m instances of item
+ 4 5 nil


)

You might also like