[Haskell-beginners] Heterogeneous Lists

Brent Yorgey byorgey at seas.upenn.edu
Tue May 28 15:49:24 CEST 2013

On Tue, May 28, 2013 at 07:36:13AM +0000, harry wrote:
> Every OO language which supports generics allows a declaration such as
> List<Show> alist, where Show is an interface. Any type implementing Show can
> be put in alist, and any Show operation can be performed on the alist's
> members. No casts, wrappers, or other special types and plumbing are needed.
> Why isn't it possible to do this directly in Haskell?

I am not sure what you mean by asking "why".  If you are simply
implying that this is stupid and Haskell *should* be able to do this
directly, then I would say that you need to spend more time learning
to think through the lens of the abstractions Haskell *does* give you,
and stop trying to think through an OOP lens.  I can count on one
finger the number of times I have wanted a heterogeneous collection
like this --- it was a long time ago, and I was wrong.

On the other hand, if you sincerely want to know why, it is simply
that this kind of feature does not fit nicely within the space of the
design decisions that were made when creating Haskell, for example:
(1) All the elements of a list must have the same type.  (2) A type
class cannot be used as a type.  (3) Haskell has no subtyping.  And
there are good reasons behind these decisions as well.  For example,
subtyping makes type inference nigh impossible.


More information about the Beginners mailing list