Kind of expression | Examples | How to evaluate |
---|---|---|
Value | 17, true, 'arugula | You have the value already. |
Placeholder | pi, + | Get its defined value. |
Function application | (+ (* 9 4) pi), (fahr->celc -40) | Evaluate the subexpressions
|
Special form |
|
Various, but not shocking. See below for cond, define, define-struct, and finally and/or. |
Note that the first part, about placeholders, explains why we see the test output as we do, when we write (times-served empty) = 0. Note that a slightly nicer way to write your test cases could be: (= (times-served empty) 0). (What would print out?)
Note anything familiar? The rules of evaluation are recursive. If you wanted, you could make your own data definitions to represent (your versions of) numbers, placeholders, function-call, and define; then you could use the design recipe, and write a scheme evaluator -- the core of drscheme -- yourself! (After seeing mutually-recursive data structures next week, this is a reasonable week-4 scheme homework.) Elegance speaks for itself!
Note: Over the rest of the semester, we will learn (just) three more special forms. (The formal definition of the corresponding part of C++ or Java requires about dense 30 pages, instead of 1. Those specifications have also been found to contain inconsistencies and errors, which have taken a long time to get revised.)
Digression: the proper use of quotation marks.
(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.
(define some-list ...) (cond [(empty? some-list) 0] [(cons? some-list) (first some-list)]) (define n ...) (define sum ...) ; Find the average of "n" things adding to "sum": ; (cond [(zero? n) 'average-not-defined] [else (/ sum n)])