Hterogeneous lists in Haskell

11 10 2007

Sometime ago I needed something that Haskell don’t have by default: heterogeneous lists…

After think about that I decide to make my own “module” so I did this:

data Hetero where
        H :: forall a . Show a => a -> Hetero
instance Show Hetero where
        show (H s) = show s

In fact is just a data type and an instance to show the data. Nothing more, nothing less…

Here some examples of heterogeneous lists:

list , list2 :: [Hetero]
list = [H ['a'..'z'], H 10, H '0', H 3.1, H [1..10]]
list2 = [H [H [H '0', H [1..10]], H '0'], H 1]

By the way, the data type is write using Generalised Algebraic Data-Types (GADT). GADT is, in fact a very recent thing in Haskell, I will talk about that latter.

Some time ago, after doing this, I was searching GADT and find this after I find another one talking about forall's and exists in Haskell, in that one, on the first example you can learn more about Heterogeneous Lists in Haskell and Haskell…

NOTE: Today I preferred blue.

code here


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: