Professional Documents
Culture Documents
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
[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
)