Guest User

Untitled

a guest
Jul 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import Data.List
  2.  
  3. -- Given three data types...
  4. data A = A deriving (Show)
  5. data B = B deriving (Show)
  6. data C = C deriving (Show)
  7.  
  8. -- and a unifying type that covers them all...
  9. data Covered
  10. = CoveredA A
  11. | CoveredB B
  12. | CoveredC C
  13.  
  14.  
  15. -- what's the best way to write this function?
  16. extract :: [Covered] -> ([A], [B], [C])
  17. -- my best guess follows.
  18. extract covereds = (as, bs, cs) where
  19. (coveredAs, others) = partition isA covereds
  20. (coveredBs, coveredCs) = partition isB others
  21. isA (CoveredA _) = True
  22. isA _ = False
  23. isB (CoveredB _) = True
  24. isB _ = False
  25. as = map (\(CoveredA a) -> a) coveredAs
  26. bs = map (\(CoveredB b) -> b) coveredBs
  27. cs = map (\(CoveredC c) -> c) coveredCs
  28.  
  29.  
  30. testData = [CoveredC C, CoveredA A, CoveredB B, CoveredC C, CoveredB B, CoveredA A]
  31. main = print $ extract testData
Add Comment
Please, Sign In to add comment