variable --> < var > = x | area-of-disk | perimeter | .... constant --> < con > = true | false 'a | 'doll | 'sum | ... 1 | -1 | 3/5 | 1.22 | ... primitive --> < prm > = + | - | ...
definition --> < def > = (define (< var > < var > ... < var >) < exp >)) expression --> < exp > = < var > | < con > |( < prm > < exp > ... < exp > ) |( < var > <exp > ... < exp > ) |(cond ( < exp > < exp < ) ... ( < exp > < exp > )) |(cond ( < exp > < exp > ) .. else < exp > ))
Symbol ---> 'all variable ---> x program application ---> (x x)
The components of compound statements have names of their own:
Some people think of a program definition in similar terms to a function definition, with a left side (the header) and a (the body) right side.
We often call the header's first component the function and the remaining of the components in the header the arguments.
A cond-expression consists of cond-lines and cond-clauses.
(define (f x ...) exp) and f, x, ... are variables and exp some (legal) expression. Then an application of a program is governed by the law: (f v ...) = exp where v ... is a sequence of values that is as long as the sequence x ... in the definition of f.
define (poly x y) (+ (expt 2 x) y)) becomes (poly 3 5) = (+ (expt 2 3) 5)) = 2 3 + 5 = 8 + 5 = 13
when the first condition is false:
(cond
[false ...]
[exp1 exp2]
...)
= (cond
; The first line disappeared.
[exp1 exp2]
...)
then the first cond-line disappears;
when the first condition is true:
(cond
[true exp]
...)
= exp
the entire cond-expressions is replaced by the first answer;
when the only line left is the else-line:
(cond
[else exp])
= exp
the cond-expressions is replaced by the answer in the else-clause.
(cond
[false 1]
[true (+ 1 1)]
[else 3])
= (cond
[true (+ 1 1)]
[else 3])
= (+ 1 1)
= 2 (define f (x) (x * x))contains two separate syntax errors (where are they?)
(+ (* 20 2) (/ 1 (- 10 10)))proceeds as follows:
= (+ (* 20 2) (/ 1 (- 10 10))) = (+ 40 (/ 1 0)) = /: divide by zeroThis is a message telling the programmer about the run-time error
; my-divide : number -> number
(define (my-divide n)
(cond
[(= n 0) 'inf]
[else (/ 1 n)])) (define < var > < exp > )
(define Radius 5) (define Area (* (* Radius Radius) pi))
(define-struct < var > ( < var > ... < var > )
(define-struct point (x y z)) .
(define-struct (point x y z))and
(define-struct point x y z)are improper definitions because define-struct is not followed by a single variable name and a sequence of variables in parentheses.