Skip to content

Instantly share code, notes, and snippets.

@vertexcite
Last active August 29, 2015 14:08
Show Gist options
  • Save vertexcite/69f30ef3f900db73dd15 to your computer and use it in GitHub Desktop.
Save vertexcite/69f30ef3f900db73dd15 to your computer and use it in GitHub Desktop.
Heterogeneous List in Haskell (simple version)
{-# LANGUAGE ExistentialQuantification #-}
-- This is adapted from http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types
-- See also https://www.haskell.org/haskellwiki/Heterogenous_collections#Existential_types
module HeterogeneousList where
data ShowBox = forall s. Show s => SB s
heteroList :: [ShowBox]
heteroList = [SB (), SB (5::Integer), SB True]
instance Show ShowBox where
show (SB s) = show s -- (*) see the comment in the text below
f :: [ShowBox] -> IO ()
f = mapM_ print
main :: IO ()
main = f heteroList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment