[racket] polymorphic functions

From: Greg Hendershott (greghendershott at gmail.com)
Date: Thu Oct 11 16:34:09 EDT 2012

> I realized recently that one thing I miss in Racket are polymorphic core
> functions (car, cdr, map, etc.). So I was wondering what it would take to
> make them polymorphic. What is the best way to implement a polymorphic
> function in Racket? Any ideas, suggestions and critics are mighty welcome.

I'm also not sure exactly what you mean by "polymorphic". Matthias
talked about generics.

When you mention "car, cdr, map, etc.", I wonder if you might want to
know about sequences, iterations, and comprehensions.

(define a-list (list 1 2 3))
(define a-vector (vector 1 2 3))

The following will work if `seq' is either one:

(for ([x seq])
  ... do something with x ...)

[Although if you know it will always be a specific kind of sequence,
you can use a "hint" to make it faster:

(for ([x (in-list a-list)]) ...
(for ([x (in-vector a-vector)]) ...]

There are many more kinds of sequences besides lists and vectors, and
you can define your own.

More info:
Sequences: http://pre.racket-lang.org/docs/html/reference/sequences.html
Iterations and comprehensions:

P.S. IIUC, sequences recently were reimplemented using generics. So I
suppose I'm talking about a specific facet of what Matthias was
already talking about.

P.P.S. My background in C++ made me think I would miss "polymorphism"
in the sense of class inheritance. It turned out I didn't miss it at
all. Although if I did miss it, Racket does have classes and objects.

Posted on the users mailing list.