灯下 登录
计算机科学 / SICP / 2.5.1 Generic Arithmetic Operations

Exercise 2.78 · 习题

Exercise 2.78: The internal procedures in the

scheme-number package are essentially nothing more than calls to the

primitive procedures +, -, etc. It was not possible to use the

primitives of the language directly because our type-tag system requires that

each data object have a type attached to it. In fact, however, all Lisp

implementations do have a type system, which they use internally. Primitive

predicates such as symbol? and number? determine whether data

objects have particular types. Modify the definitions of type-tag,

contents, and attach-tag from 2.4.2 so that our

generic system takes advantage of Scheme’s internal type system. That is to

say, the system should work as before except that ordinary numbers should be

represented simply as Scheme numbers rather than as pairs whose car is

the symbol scheme-number.

练习 2.78:scheme-number 包中的内部过程本质上只是对基本过程 +、- 等的调用。之所以不能直接使用语言的基本过程,是因为我们的类型标签系统要求每个数据对象都附带一个类型。然而事实上,所有 Lisp 实现内部都有自己的类型系统,并在内部使用它。基本谓词(如 symbol? 和 number?)用于判断数据对象是否具有特定类型。修改 2.4.2 节中 type-tag、contents 和 attach-tag 的定义,使我们的通用系统能够利用 Scheme 的内部类型系统。也就是说,系统的行为应与原来相同,但普通数应直接表示为 Scheme 的数,而不是以 scheme-number 为 car 的序对。