[Haskell-cafe] lists with mixed types
oleg at pobox.com
oleg at pobox.com
Sat Jun 16 03:59:32 EDT 2007
Anatoly Yakovenko wrote:
> but what i really want to do is just do
> map func [1, 2.0]
> [1, "2.0"]
>
> I understand that this is impossible in haskell,
If you use a heterogeneous list, it is possible. The HList paper
describes such examples.
http://homepages.cwi.nl/~ralf/HList/
> but why cant the
> compiler generate the Foo and Bar data types for me and just require
> that i have a func defined for Int -> Int and Float -> String?
It can. One can write something like
lst = (1::Int) `eCons` True `eCons` 'a' `eCons` eNil
the result is a true Haskell list of the type
[Either Int (Either Bool Char)]
and the contents
[Left 1, Right (Left True), Right (Right 'a')]
Haskell figures out the union (Either) type automatically. Using a
function like hOccurs one can `downcast' from that union type to a
specific type (Int or Char) so one does not need to care about the
number and amount of Left and Right...
In more detail, this is described in Section 5.5 of
http://homepages.cwi.nl/~ralf/OOHaskell
and code at
http://darcs.haskell.org/OOHaskell/ShapesEither.hs
More information about the Haskell-Cafe
mailing list