Write a typeof procedure in Scheme
Help me answer the following question appears in Simply Scheme
6.7 Write a procedure type-of that takes anything as its argument an开发者_运维百科d returns one of the words word, sentence, number, or boolean:
> (type-of '(getting better))
SENTENCE
> (type-of 'revolution)
WORD
> (type-of (= 3 3))
BOOLEAN
(Even though numbers are words, your procedure should return number if its argument is a number.)
You can use the form cond
to check several conditions and execute an action accordingly. You can use the predicates boolean?
, number?
, word?
and sentence?
¹ to find out whether a value is a boolean, number, word or sentence respectively. That's basically all there is to it.
The only thing you need to consider is that the case for number?
must come before the case for word?
(because word?
would also return true for numbers as the exercise helpfully points out).
¹ The first two are standard scheme, the latter two are defined in simply.scm, which comes with the book.
精彩评论