[plt-scheme] Printing and Overloading

From: Robby Findler (robby at cs.uchicago.edu)
Date: Wed Mar 17 20:28:12 EST 2004

You can do this for +

(module super-scheme mzscheme
  (provide (rename super+ +)
           (all-from-except mzscheme +)
           (struct tuple (l r)))
  
  (define-struct tuple (l r))
  
  (define (super+ a b)
    (cond
      [(and (number? a) (number? b))
       (+ a b)]
      [(and (tuple? a) (tuple? b))
       (make-tuple (+ (tuple-l a) (tuple-l b))
                   (+ (tuple-r a) (tuple-r b)))]
      [else
       (error '+ "expected args that were either both tuples or both numbers, got ~e and ~e"
              a b)])))

(module m super-scheme
  (define t1 (make-tuple (+ 1 1) (+ 1 1)))
  (define t2 (make-tuple (+ 2 2) (+ 3 3)))
  (define t3 (+ t1 t2)))

The super-scheme module acts just like mzscheme when used as a module
argument, except it change the behavior of + and provides the tuple
struct.

As for the printer, we're still working on a way to do that, but you
can change many aspects of printing if you're willing to use (lib
"pretty.ss")'s pretty-print function (that's what DrScheme's repl uses,
for example, rather than `write').

Robby

At Thu, 18 Mar 2004 01:17:08 +0000, 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).
> 
> 2. Print the tuple so that (write (make-tuple 2 3)) or even (format "~a"
> (make-tuple 2 3)) prints something like "(2, 3)".
> 
> Is there anyway to do this in PLT-Scheme? If there is, can somebody
> please explain it or direct me to the main references? 
> 
> Cheers, 
> -- 
> 
> Paulo J. Matos : pocm [_at_] mega . ist . utl . pt
> Instituto Superior Tecnico - Lisbon
> Computer and Software Eng. - A.I.
>  - > http://mega.ist.utl.pt/~pocm
> ---
>         -> God had a deadline...
>                 So, he wrote it all in Lisp!


Posted on the users mailing list.