MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -> m a writer ~(a, w) = do tell w return a tell :: w -> m () tell w = writer ((),w) listen :: m a -> m (a, w) pass :: m (a, w -> w) -> m a lis…
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 (<<<) = (.) (&…
Comprehension Extensions 关于解析式的相关语言扩展. List and Comprehension Extensions 24 Days of GHC Extensions: List Comprehensions ParallelListComp Prelude> [(w,x,y,z) | ((w,x),(y,z)) <- zip [(w,x) | w <- [1..3], x <- [2..4]] [(y,z) | y <- [3..5], z &…
安装 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…