【发布时间】:2016-12-14 17:20:56
【问题描述】:
我开始掌握函子、应用程序和单子。
这些例子只是为了说明基础:
data User a = Age a deriving (Show, Eq, Ord)
Functor(将非上下文函数应用于单个上下文数据类型):
instance Functor User where
fmap f (Age a) = Age (f a)
functorFunction :: Int -> Int
functorFunction e = e + 1
main = do
print $ fmap functorFunction (Age 22)
Applicative(将简单函数应用于多种上下文数据类型):
instance Applicative User where
pure a = Age a
(<*>) (Age a) = fmap a
applicativeFunction :: Int -> Int -> Int
applicativeFunction e1 e2 = e1 + e2
main = do
print $ pure (applicativeFunction) <*> (Age 44) <*> (Age 65)
我已经通过learnyouahaskell 并没有能够找到一个简单的解释
1) 如何为我的“用户 a”类型定义 monad 结构,以及 2) monad 提供什么功能,例如,应用函子?
【问题讨论】:
-
您的
User类型与theIdentityfunctor 同构。看看its source code -
关于2,见this。
-
或this。