### Unifying Scheme numbers and arithmetic

Thursday, July 31st, 2008Although *display* could output all three number formats, *read* could not. I’ve now added the necessary code to read in negative numbers, and to create an absolute zero number.

The procedures for addition and subtraction have been modified to remove digit constants. As an example, the nine’s complement procedure now looks like this…

(nines-complement (lambda (x) (cond ( (eq? x digit0) digit9 ) ( (eq? x digit1) digit8 ) ( (eq? x digit2) digit7 ) ( (eq? x digit3) digit6 ) ( (eq? x digit4) digit5 ) ( (eq? x digit5) digit4 ) ( (eq? x digit6) digit3 ) ( (eq? x digit7) digit2 ) ( (eq? x digit8) digit1 ) ( (eq? x digit9) digit0 ) ) ) )

The result is that I can now use the numbers generated by *read* and outputted by *display* by changing some *letrec* definitions…

(num-mark (car 1)) (psign (car(cdr 1))) (msign (car(cdr -1))) (zsign (car(cdr 0))) (digit0 (integer->octet 0)) (digit1 (integer->octet 1)) (digit2 (integer->octet 2)) (digit3 (integer->octet 3)) (digit4 (integer->octet 4)) (digit5 (integer->octet 5)) (digit6 (integer->octet 6)) (digit7 (integer->octet 7)) (digit8 (integer->octet 8)) (digit9 (integer->octet 9))

With the *add* procedure doing both addition and subtraction of magnitudes, the *subtract* procedure requires only the additional support of a negation procedure.

(subtract (lambda (x y) (cond ( (eq? (car x) num-mark) (cond ( (eq? (car y) num-mark) (sub-num x y) ) ( #t '***undefined*** ) ) ) ( #t '***undefined*** ) ) ) ) (sub-num (lambda (x y) (add-num x (negate y)) ) ) (negate (lambda (x) (cond ( (eq? (car(cdr x)) zsign) x ) ( (eq? (car(cdr x)) psign) (cons num-mark (cons msign (cdr(cdr x)))) ) ( (eq? (car(cdr x)) msign) (cons num-mark (cons psign (cdr(cdr x)))) ) ( #t '***undefined*** ) ) ) )