define
(define placeholder exp)
.
Why define
is a special form:
What if we tried this as a normal function,
which evaluates all its arguments first?
(define (placeholder ..args..) body-exp)
. define
).
cond
(cond [question-a answer-a] [question-b answer-b] ... [question-z answer-z])Meaning: Evaluate question-a. If it is
true
(or, the keyword else
),
then the value of the entire cond is
what we get by evaluating the expression answer-a.
If question-a evaluated to false,
then the value of the entire cond is:
(cond [question-b answer-b] ... [question-z answer-z])(Cf. Recurring on a list.) It is an error to have a
cond
with no question/answer pairs,
or if question-a doesn't evaluate to a be boolean.
Why is cond
special form?:
Suppose we tried to have cond
be a regular function,
where we evaluate all questions and all answers, before even
invoking cond
.
What would happen to the following code?:
(define some-list ...) (cond [(empty? some-list) 0] [(cons? some-list) (first some-list)]) ;;;;; Or, consider: (define n ...) (define sum ...) ; Find the average of "n" things adding to "sum": ; (cond [(zero? n) 'average-not-defined] [else (/ sum n)])
define-struct
(define-struct struct-name (field-1 .. field-n))
,
where the struct-name
and each field-i
are placeholders.
As mentioned, this just causes Scheme to create
the constructor make-struct-name
,
the selectors
struct-name-field-1
,
..
struct-name-field-n
,
and the predicate struct-name?
.
and
, or
(and arg1 arg2)
.
Meaning:
Evaluate arg1
first.
If it is false
, return false
.
Otherwise, evaluate arg2
,
and return it.
(Similarly for or
, but replace false
with true
.)
This is called "short-circuiting" -- returning an answer
even before evaluating all their arguments.
*
could be specially written so that
if its first argument is 0, then it returns 0 without
evaluating further arguments.
But that's not what is does.
and
a special form:
consider (and (symbol? a-fo) (symbol=? a-fo 'gnat))
.
If a-fo
is not a symbol, what happens?