[plt-scheme] Printing and Overloading
Paulo Jorge de Oliveira Cantante de Matos wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hi all,
>
> I've been thinking about this lately bout I've not had the time to try
> things out. However, as the result of some hours of research I found
> nothing in PLT Scheme that allows me to easily do one of the following
> things:
>
> Imagine I have a structure/class tuple (x1, x2) and I want to:
>
> 1. Overload primitive +, so that (+ (make-tuple 2 3) (make-tuple 2 3))
> gives me (4, 6).
>
(require (lib "plt-match.ss"))
(define-struct tuple (x y) (make-inspector))
(define +
(let ((old+ +))
(match-lambda*
((list (and t (? tuple?)) ...)
(make-tuple
(apply + (map tuple-x t))
(apply + (map tuple-y t))))
(else (apply old+ else)))))
> 2. Print the tuple so that (write (make-tuple 2 3)) or even (format "~a"
> (make-tuple 2 3)) prints something like "(2, 3)".
>
Similar to above. Just capture the old function before you rename it.
Bruce