2.78 Exercise 2.78
We are asked to remove a layer of our typing for pure Scheme numbers, knowing
that the language is capable of discerning whether an object is a number. We
can do this by adding calls to number? to attach-tag, type-tag,
and contents, probably like this:
(define (type-tag datum) | (if (pair? datum) | (car datum) | (if (number? datum) | 'scheme-number | (error "Bad tagged datum -- TYPE-TAG" datum)))) |
|
(define (contents datum) | (if (pair? datum) | (cdr datum) | (if (number? datum) | datum | (error "Bad tagged datum -- CONTENTS" datum)))) |
|
(define (attach-tag type-tag contents) | (if (and | (eq? type-tag 'scheme-number) | (number? contents)) | contents | (cons type-tag contents))) |
|
I’ve elected to check the type of contents and the given type-tag
in attach-tag for extra safety.