Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. {-# LANGUAGE LambdaCase #-}
  2. {-# LANGUAGE TypeFamilies #-}
  3.  
  4. import Control.Comonad.Cofree
  5. import Control.Monad.Free
  6. import Data.Functor.Foldable
  7.  
  8. oddIndices :: [a] -> [a]
  9. oddIndices = histo $ \case
  10. Nil -> []
  11. Cons h (_ :< Nil) -> [h]
  12. Cons h (_ :< Cons _ (t :< _)) -> h:t
  13.  
  14. evenIndices :: [a] -> [a]
  15. evenIndices = histo $ \case
  16. Nil -> []
  17. Cons _ (_ :< Nil) -> []
  18. Cons _ (_ :< Cons h (t :< _)) -> h:t
  19.  
  20. oddIndicesF :: [a] -> [a]
  21. oddIndicesF = futu coalg where
  22. coalg list = case project list of
  23. Nil -> Nil
  24. Cons x s -> Cons x $ do
  25. return $ case project s of
  26. Nil -> s
  27. Cons _ t -> t
  28.  
  29. evenIndicesF :: [a] -> [a]
  30. evenIndicesF = futu coalg where
  31. coalg list = case project list of
  32. Nil -> Nil
  33. Cons _ s -> case project s of
  34. Nil -> Nil
  35. Cons h t -> Cons h $ return t
  36.  
  37. nil :: Free (Prim [a]) b
  38. nil = liftF Nil
  39.  
  40. cons :: a -> b -> Free (Prim [a]) b
  41. cons h t = liftF (Cons h t)
  42.  
  43. twiddle :: [a] -> [a]
  44. twiddle = futu coalg where
  45. coalg r = case project r of
  46. Nil -> Nil
  47. Cons x l -> case project l of
  48. Nil -> Cons x nil
  49. Cons h t -> Cons h $ cons x t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement