Created
February 5, 2012 00:33
-
-
Save NicolasT/1741466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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