This was probably discussed in this forum before. Has anyone succeeded adding multiple dispatch capability to newLisp using a macro or otherwise?
http://en.wikipedia.org/wiki/Multiple_d ... ommon_Lisp
If newLisp had a feature which allowed parametric polymorphism, my code will look more cleaner as it grows in size.
Multiple dispatch?
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: Multiple dispatch?
It might not satisfy a purist, but you may implement it via "dual" contexts and a faked FOOP object, as in the following example:
Here the dual function simply creates the appropriate FOOP composition context to allow its "singular" polymorphism to apply.
Code: Select all
(define (dual a b) (list (context (sym (string a "-" b) MAIN))))
(define (collide-with a b) (:collide-with (dual (a 0) (b 0)) a b))
(context 'MAIN:asteroid-asteroid)
(define (collide-with a b) (list "BANG" (context) a b))
(context 'MAIN:asteroid-spaceship)
(define (collide-with a b) (list "BONG" (context) a b))
(context 'MAIN:spaceship-asteroid)
(define (collide-with a b) (list "ZING" (context) a b))
(context 'MAIN:spaceship-spaceship)
(define (collide-with a b) (list "POFF" (context) a b))
Re: Multiple dispatch?
This doesn't work. I get the following instead
When I was expecting "BANG"
Code: Select all
(collide-with "asteroid" "asteroid") =>
("POFF" spaceship-spaceship "asteroid" "asteroid")
Re: Multiple dispatch?
Try this fixed up version:
Part of the problem with the original was it expected the arguments to be strings embedded in a list.
Code: Select all
(context 'MAIN:asteroid-asteroid)
(define (collide-with a b) (list "BANG" (context) a b))
(context 'MAIN:asteroid-spaceship)
(define (collide-with a b) (list "BONG" (context) a b))
(context 'MAIN:spaceship-asteroid)
(define (collide-with a b) (list "ZING" (context) a b))
(context 'MAIN:spaceship-spaceship)
(define (collide-with a b) (list "POFF" (context) a b))
(context 'MAIN)
(define (dual a b) (list (context (sym (string a "-" b) MAIN))))
(define (collide-with a b) (:collide-with (dual a b) a b))
(println (collide-with "asteroid" "asteroid"))
(exit)
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: Multiple dispatch?
Well, yes, I had assumed one would be dealing with "typed" arguments, such as, say, (android 544) and (spaceship 345) etc, and not just argument types. In any case the point is to map the apparent plurality into a singular form, and then use the FOOP polymorphic dispatch.