Our current definition of the Beginning Student Scheme language omits two forms of expressions: and and or expressions. Adding them provides a case study of how to study new language construct. We must first understand their syntax, then their semantics, and finally their pragmatics.
Here is the revised grammar:
The grammar says that and and or are keywords, each followed by two expressions. At first glance, the two look like (primitive or function) applications. To understand why they are not, we must look at the pragmatics of these expressions first.
Suppose we need to formulate a condition that determines whether the n-th fraction of 1 is m:
(and (not (= n 0))
(= (/ 1 n) m))
We formulate the condition as an and combination of two boolean
expressions, because we don't wish to divide by 0 accidentally.
Next, assume n becomes 0 during the course of the
evaluation. Then the expression becomes
(and (not (= 0 0))
(= (/ 1 0) m))
Now, if and were an ordinary operation, we would have to evaluate
both subexpressions, which would trigger an error in the second one. For
this reason, and is not a primitive operation,
but a special
expression. In short, we use and and or expressions to
combine boolean computations that may have to short-cut an evaluation.
Once we understand how and and or expressions should be evaluated, it is easy to formulate matching rules. Better still, we can formulate expressions in our first language that are equivalent to these expressions:
(andand![]()
)
(cond [
![]()
] [else false])
(orThese equivalences simplify what actually takes place in DrScheme but they are a perfectly appropriate model for now.![]()
)
(cond [
true] [else
])