zoukankan      html  css  js  c++  java
  • Functor、Applicative 和 Monad x

    首先,我们来看一下 Functor typeclass 的定义:

    1

    2

    class Functor f where

        fmap :: (a -> b) -> f a -> f b

    Functor typeclass fmap (a -> b) f a f b f Functor :fmap 函数可类比 Swift 中的 map 方法。

    Applicative typeclass

    同样的,我们先来看一下 Applicative typeclass 的定义:

    1

    2

    3

    class Functor f => Applicative f where

        pure :: a -> f a

        (<*>) :: f (a -> b) -> f a -> f b

    我们注意到,与 Functor typeclass 的定义不同的是,在 Applicative typeclass 的定义中多了一个类约束 Functor f ,表示的意思是数据类型 f 要实现 Applicative typeclass 的前提条件是它必须要实现 Functor typeclass ,也就是说它必须是一个 Functor 。

    在 Applicative typeclass 中定义了两个函数:

    • pure a (<*>) :将一个在上下文中的函数 f (a -> b) 应用到一个在上下文中的值 f a ,并返回另一个在上下文中的值 f b 。

    同样的,我们先来看一下 Monad typeclass 的定义:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    class Applicative m => Monad m where

        return :: a -> m a

        (>>=) :: m a -> (a -> m b) -> m b

        (>>) :: m a -> m b -> m b

        x >> y = x >>= \_ -> y

        fail :: String -> m a

        fail msg = error msg

    Monad typeclass return(>>=)(>>) fail (>>) fail Monad typeclass

    1

    2

    3

    class Applicative m => Monad m where

        return :: a -> m a

        (>>=) :: m a -> (a -> m b) -> m b

    怎么样?现在看上去就好多了吧。跟 Applicative typeclass 的定义一样,在 Monad typeclass 的定义中也有一个类约束 Applicative m ,表示的意思是一种数据类型 m 要成为 Monad 的前提条件是它必须是 Applicative 。另外,其实 return 函数的功能与 Applicative 中的 pure 函数的功能是一样的,只不过换了一个名字而已,它们的作用都是将一个值 a 放入上下文中。而 (>>=) 函数的功能则是应用一个(接收一个普通值 a 但是返回一个在上下文中的值 m b 的)函数 (a -> m b) 到一个上下文中的值 m a ,并返回另一个在相同上下文中的值 m b 。

  • 相关阅读:
    适度平衡树
    适度平衡树
    卡特兰(Catalan)数列
    卡特兰(Catalan)数列
    从二叉搜索树到平衡二叉搜索树
    java开发环境配置(windows下JDK7+tomcat7)
    Spring整合Hibernate的步骤
    C++重载加号运算符实现两个结构体的相加
    AABB包围盒、OBB包围盒、包围球的比較
    Html的空格显示
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9829224.html
Copyright © 2011-2022 走看看