List types
Fergus Henderson
fjh@cs.mu.oz.au
Sat, 2 Dec 2000 13:28:19 +1100
On 01-Dec-2000, Eric Allen Wohlstadter <wohlstad@cs.ucdavis.edu> wrote:
> Is there a way to make a list that contains multiple types? If not, why,
> and isn't this a serious restriction? I would like to be able to say:
>
> map show ['a',5]
>
> Since both Char and Num derive Show it seems like a reasonable thing to
> do.
You can't do it in standard Haskell, but with ghc extensions you can
achieve the same effect. For example, the following program
data Showable = forall a . Show a => Showable a
instance Show Showable where
show (Showable x) = show x
example = map show [Showable 'a', Showable 5]
main = print example
produces the output
["'a'","5"]
See http://www.haskell.org/ghc/docs/latest/set/existential-quantification.html
for more details.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.