Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created February 5, 2012 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicolasT/1741466 to your computer and use it in GitHub Desktop.
Save NicolasT/1741466 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
-- See http://www.reddit.com/r/haskell/comments/pb7dd/how_would_i_write_a_haskell_function_to_do_this/
import Data.Data
data Foo = Bar Int
| Baz String
deriving (Show, Eq, Typeable, Data)
hasTypes :: Data a => [a] -> [Constr] -> Bool
hasTypes vs ts = all (uncurry (==)) (zipWith (\v t -> (toConstr v, t)) vs ts)
main :: IO ()
main = do
print $ hasTypes [Bar 1, Bar 2, Baz "A"] [r, r]
print $ hasTypes [Bar 1, Baz "A", Bar 2] [r, z]
print $ hasTypes [Bar 1, Baz "A"] [z, r]
print $ hasTypes [Bar 1] []
where
r = toConstr $ Bar undefined
z = toConstr $ Baz undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment