[plt-scheme] A matter of design
On 3/21/07, Daniel Yoo <dyoo at cs.wpi.edu> wrote:
> > On the core side we will have a huge issue... integer values defines
> > basic operations on integers +, -, etc with attached contracts and puts
> > this all in the functions hash-table. Then comes the matrix value and
> > defines +, -, * on matrices but with different contracts. Now the user
> > does (+ matrix matrix) or something... how is the core supposed to know
> > which + it is talking about (not to mention the best way to keep this.
>
> Hi Paulo,
>
> This feels very similiar to the "generic operations" stuff in:
>
> http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-18.html#%25_sec_2.5
>
>
Great read!
Thank you so much for the reference. In fact, I had the book here
because It was recommended by my teacher when I had introduction to
programming, using Scheme almost 8 years ago. Time to give it a
serious read.
Cheers,
Paulo Matos
> 'swindle' provides library support for these kinds of generic operations.
> Here's a quick and dirty example:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module test-generics mzscheme
> (require (lib "clos.ss" "swindle"))
>
> (defgeneric plus)
>
> (defmethod plus ((x <number>) (y <number>))
> (+ x y))
>
> (defmethod plus ((x <string>) (y <string>))
> (string-append x y))
>
> (printf "~a~n" (plus 4 2))
> (printf "~a~n" (plus "4" "2")))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
>
>
--
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK