# [racket] polymorphic functions

 From: Greg Hendershott (greghendershott at gmail.com) Date: Thu Oct 11 16:34:09 EDT 2012 Previous message: [racket] polymorphic functions Next message: [racket] polymorphic functions Messages sorted by: [date] [thread] [subject] [author]

```> 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

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.

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

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