[racket] About genericity...
On 04/04/2014 23:48, Alexander D. Knauth wrote:
>> (def-multi +)
>>
>> (add-multi + '() (lambda () 0))
>> (add-multi + (listof number?) (lambda args (apply + args)))
>> (add-multi + (listof vector?) (lambda args (apply v+ args)))
>>
>> (check-equal? (+ 1 2 3) 6)
>> (check-equal? (+ (vector 1 2 3)
>> (vector 2 3 4)
>> (vector 3 4 5))
>> (vector 6 9 12))
I copied Common Lisp's model, but it's a good idea to add Racket-style
contracts. It should be a matter of simply adding a matcher for this.
In the meantime, this will work:
(def-multi + multi-match-parms)
(add-multi + (list number?) (lambda args (apply rkt:+ args)))
(add-multi + (list vector?) (lambda args (apply v+ args)))
(add-multi + (list) (lambda () 0))
(check-equal? (+ 1 2 3) 6)
(check-equal? (+ (vector 1 2 3)
(vector 2 3 4))
(vector 3 5 7))
Note that you forgot to prefix the original + with rkt: in your
procedure, and that v+ only accepts 2 vectors.
Regards,
-Patrick