[Haskell-beginners] User-defined polymorphic data type: heterogeneous list?

Christopher Howard christopher.howard at frigidcode.com
Mon Jul 11 07:00:51 CEST 2011


I'm trying to understand parametric polymorphism and polymorphic data 
types. I especially want to go beyond simply using the defined 
polymorphic data types (lists and so forth) and see how I can make my 
own useful ones.

As my first stab at it, it seemed like I should be able to create my own 
heterogeneous "list" data type -- i.e., a "list" data type that can 
contain elements of different types. (like, [3,'a',True], for example)

But I'm a little stuck. My first try was like so:

data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)

...but this of course did not work, because all elements end up having 
to be the same type.

Then I tried

data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)

...but this doesn't work because every other other element has to be the 
same type:

Element 'a' (Element 1 (Element 'a' (Element 2 Null)))

...I could go on and embarrass myself some more, but since I'm likely 
widely off-base I'll just ask if somebody can point me in the right 
direction.

-- 
frigidcode.com
theologia.indicium.us



More information about the Beginners mailing list