[plt-scheme] Printing and Overloading

From: Jacob Matthews (jacobm at cs.uchicago.edu)
Date: Wed Mar 17 21:02:57 EST 2004

Bruce Hauman wrote:

> Hey again,
>
> Here is a version without the match syntax.
>
> (define-struct tuple (x y) (make-inspector))
>
> (define +
>   (let ((old+ +))
>     (lambda args
>       (if (andmap tuple? args)
>           (make-tuple
>            (apply + (map tuple-x args))
>            (apply + (map tuple-y args)))
>           (apply old+ args)))))

There's a subtle bug here: (+) will infinite loop due to andmap 
vacuously holding for an empty list. I'd prefer that (+) return 0 rather 
than (make-tuple 0 0), so I'd probably fix the problem by making the 
condition (and (pair? args) (andmap tuple? args)) rather than just 
(andmap tuple? args).

-jacob



Posted on the users mailing list.