( ) i-am-a-name i-am! define cond ! @ % ^ & * + 888
program = (define (function-name variable-name ...) expression) ... (define variable-name expression) ... expression
expression = | variable-name | constants | (primitive-operation expression ... ) | (function-name expression ... ) | (cond (expression expression) ... (expression expression)) | (cond (expression expression) ... (else expression))
constants = | 0 | +1 | -1 | ... | #t | #f | 'hello | 'world | empty
variable-name = name function-name = name name = x | f | ! | @ | ...
All red things are what we write down. Let's practice with this grammar. It's an extremely simple game -- match patterns.
! (! 10) (define (! n) n) (cond (test 'hello)) (cond (else 10))
Grammatical mistakes:
(define (f x) + x y z) (cond) (define (f test) (cond ((test) 10) (else 0)))Why are these grammatical mistakes? Computing scientists call them syntactic mistakes.
names for pieces of "sentences"
In Scheme without lists, we can safely say numbers and symbols are values. Right?
the "grade school" laws:
(+ 1 1) = 2 (/ 4 2) = 2 (* 2 1) = 2 (- 4 2) = 2 (eq? 'x 'x) = #t (not #f) = #t
the "middle school" laws:
(define (f x) expression) ... (f V) = expression with all x replaced by V
An example with two variables ...
And now to the algebra of information:
for any value I and list L, (first (cons I L)) = I (rest (cons I L)) = L
But what are values:
value = 0 | +1 | -1 | ... | #t | #f | 'hello | 'world | empty | list list = empty | (cons value list)All constants are values. And lists are values.
Some strange examples
(cons (cons 1 (cons 2 empty)) empty)How many items are in this list?