Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- -- Given three data types...
- data A = A deriving (Show)
- data B = B deriving (Show)
- data C = C deriving (Show)
- -- and a unifying type that covers them all...
- data Covered
- = CoveredA A
- | CoveredB B
- | CoveredC C
- -- what's the best way to write this function?
- extract :: [Covered] -> ([A], [B], [C])
- -- my best guess follows.
- extract covereds = (as, bs, cs) where
- (coveredAs, others) = partition isA covereds
- (coveredBs, coveredCs) = partition isB others
- isA (CoveredA _) = True
- isA _ = False
- isB (CoveredB _) = True
- isB _ = False
- as = map (\(CoveredA a) -> a) coveredAs
- bs = map (\(CoveredB b) -> b) coveredBs
- cs = map (\(CoveredC c) -> c) coveredCs
- testData = [CoveredC C, CoveredA A, CoveredB B, CoveredC C, CoveredB B, CoveredA A]
- main = print $ extract testData
Add Comment
Please, Sign In to add comment