[R6RS] Draft of arithmetic SRFI

Michael Sperber sperber
Wed Aug 3 16:01:58 EDT 2005

William D Clinger <will at ccs.neu.edu> writes:

> Your code for integer addition basically looks like this:
> (define (integer+ a b)
>   (bignum+ (integer->bignum a)
>            (integer->bignum b)))
> That code can be bummed as follows:
> (define (integer+ a b)
>   (if (and (fixnum? a) (fixnum? b))
>       (let ((c (fx+ a b)))
>         (cond ((not (negative? a))
>                (if (fx<= b c)
>                    c
>                    (bignum+ (integer->bignum a) (integer->bignum b))))
>               ((negative? a)
>                (if (fx< c b)
>                    c
>                    (bignum+ (integer->bignum a) (integer->bignum c))))
>               (else c)))
>       (bignum+ (integer->bignum a) (integer->bignum b))))

Good point.

> The bummed code would not work if fx+ were to signal an error on
> overflow.  With the error-signalling semantics, it would be hard
> to do better than your original code, which I think is just too
> slow.

That I'm not seeing yet.  I'm probably overlooking something.

Assume that FX+ takes an additional argument---a binary procedure that
gets called on overflow:

(define (integer+/big a b)
  (integer+ (integer->bignum a) (integer->bignum b)))

(define (integer+ a b)
  (if (and (fixnum? a) (fixnum? b))
      (fx+ a b integer+/big)
      (integer+/big a b)))

Why would this be worse than your bummed code?

Cheers =8-} Mike
Friede, V?lkerverst?ndigung und ?berhaupt blabla

More information about the R6RS mailing list