In addition to numeric constants, Scheme has constants for the "boolean" values true and false. Try typing in #t.
> #t #t
In Scheme, #t is a constant used to represent true. There is also a constant #f that represents false. These boolean constants are like numbers in that they evaluate to themselves. In fact, this behavior characterizes constants in Scheme.
Constants in Scheme are an example of a more general kind of value called an atom. The syntax of atoms is:
[An atom is] a string of characters beginning with a letter, digit or special character other than a left "(" or right ")" parenthesis.
You can use Scheme itself to test whether various things are atoms; this is done using a predicate called . You may want to define that predicate yourself. Just execute the line:
to get basically the same procedure as Petite's. You may think this is not good enough. If so, please comment to your instructor.(define atom? (lambda (x) (not (pair? x))))
The predicate is written in front of the item to be tested and the whole expression is wrapped in parentheses, just like the arithmetic expressions from the last section. "Predicates" in Scheme are just functions (like + or *) which return Boolean values (i.e., either #t or #f). Predicates are usually given a "?" as the last character of their name as a mnemonic device.
> (atom? 1) #t > (atom? #t) #t > (atom? +) #t > (atom? (+ 1 2)) #t
Hey! Wait a minute! Didn't we just say that anything that didn't start with a left parenthesis was an atom? And doesn't that imply that anything that does start with a left parenthesis isn't an atom?
Yes and no ...
The phrase or expression (+ 1 2) is not an atom. But, in Scheme, this expression evaluates to the numeric constant 3, and that is an atom.
So what is really going on here? Recall from our discussion on the evaluation of arithmetic expressions that when we nest one expression inside another, Scheme evaluates the inner expression in order to use the resulting value in the outer expression. The same rule applies here: in order to evaluate the expression (atom? (+ 1 2)), Scheme first evaluates the inner expression (+ 1 2) to the atom 3, and then checks to see if this is an atom (which, of course, it is, so Scheme returns the value #t).
In Scheme, the difference between expressions as phrases of the language (on the one hand) and as pieces of data (on the other) is purposely somewhat blurred. Although this aspect of Scheme will turn out to be very important for us this quarter, it is also important that you understand the difference between these two things. We'll address these issues again in later sections.
As a second example of a predicate, we mention here the function, which allows you to test whether two Scheme values are equal or not. Just as with the predicate, eq? will evaluate its arguments before applying the test.
> (eq? 1 1) #t > (eq? 1 0) #f > (eq? 5 (+ 2 3)) #t > (eq? + +) #t > (eq? + *) #f > (eq? eq? eq?) #t > (eq? atom? eq?) #f
Notice that you can compare functions for equality in Scheme; if you have any background in computability theory, you will recognize that this function-comparison capability is necessarily limited, since it is undecidable whether or not any two functions are equal as functions, i.e., in the extensional sense. Scheme's notion of equality between functions is a more intensional one. If these concepts are unfamiliar to you, don't worry: these issues are not crucial, and we mention them only as a digression for those who have the relevant background.
42iscoolan atom? Why or why not?
(+ 1 2)an atom? Why or why not?
(+ 1 2)an atom? Why or why not?