Guest User

Untitled

a guest
Jun 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import Control.Arrow
  2. import Control.Arrow.CoKleisli
  3. import Control.Comonad
  4. import Control.Comonad.Reader
  5. import Data.Array
  6. import Control.Comonad.Pointer
  7. import Control.Functor.Extras
  8. import Data.Monoid
  9. import Control.Comonad.Exponent
  10. import Control.Comonad.Stream
  11. import Control.Functor.Fix
  12. import Control.Monad.Identity
  13. import Data.List
  14. import Control.Comonad.Cofree
  15. import Control.Comonad.Supply
  16.  
  17. ary :: Array Int String
  18. ary = listArray (0,2) ["hoge", "fuga", "moke"]
  19.  
  20. f, g :: Coreader Int Int -> Int
  21. f x = askC x * extract x
  22. g x = askC x * 2
  23. h :: Coreader Int Int -> Coreader Int Int
  24. h x = x =>> g =>> f
  25.  
  26. p0 :: Pointer Int String
  27. p0 = Pointer 0 ary
  28.  
  29. {-
  30. fact :: Int -> Int
  31. fact n = runExp $ factSub n $ Exp getProduct
  32. where
  33. -}
  34.  
  35. factSub :: Int -> Exp [Int] Int -> Int
  36. factSub 0 acc = extract acc
  37. factSub n acc = factSub (n-1) (acc =>> (*n) . extract)
  38.  
  39. tailS :: Stream a -> Stream a
  40. tailS st = st =>> extract . runIdentity . outCofree
  41.  
  42. streamToList :: Stream a -> [a]
  43. streamToList = unfoldr phi
  44. where
  45. phi st = Just (extract st, tailS st)
  46.  
  47. cons :: a -> Stream a -> Stream a
  48. cons a st = cofree a $ Identity st
  49.  
  50. zipWithS :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
  51. zipWithS g st1 st2 = cons (extract st1 `g` extract st2) $ zipWithS g (tailS st1) (tailS st2)
  52.  
  53. zipS :: Stream a -> Stream b -> Stream (a, b)
  54. zipS = zipWithS (,)
  55.  
  56. st1 :: Stream Int
  57. st1 = cons 1 (st1 =>> (+1) . extract)
  58.  
  59. fibs :: Stream Integer
  60. fibs = cons 1 $ cons 1 $ zipWithS (+) fibs (tailS fibs)
  61.  
  62. squareSup :: Num n => Supply n -> Supply n
  63. squareSup sup = sup =>> \x -> extract x ^ 2
  64.  
  65. allSupply :: Supply a -> [a]
  66. allSupply = map extract . split
Add Comment
Please, Sign In to add comment