Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance Category (->) where id = GHC.Base.id (.) = (GHC.Base..) (<<<) :: Category cat => cat b c -> cat a b -> cat a c (<<<) = (.) (&…
CPS (Continuation Passing Style) CPS(延续传递风格)是指函数不把处理结果作为返回值返回而是把处理结果传递给下一个函数的编码风格. 与此相对,函数把处理结果作为返回值返回的编码风格被称为直接编码风格. add :: Int -> Int -> Int add x y = x + y square :: Int -> Int square x = x * x pythagoras :: Int -> Int -> Int pythagoras…
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Free f a)) instance Functor f => Functor (Free f) where fmap f = go where go (Pure a) = Pure (f a) go (Free fa) = Free (go <$> fa) instance Functor f…
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHaskell is needed for makeLenses; RankNTypes is needed for -- a few type signatures later on. {-# LANGUAGE TemplateHaskell, RankNTypes #-} import Control…
MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a instance MonadCont (ContT r m) where callCC = ContT.callCC class Monad m => MonadCont m where MonadState 是个类型类,它为 ContT 等封装了CPS函数的 Monad 定义了通用接口. MonadSta…