简体   繁体   English

fmap的monadic“版本”的名称是什么?

[英]What is the name of the monadic “versions” of fmap?

Having a background in powershell scripting i first thought it natural to think of function composition in a pipe kind of way. 拥有powershell脚本的背景我首先想到以管道方式思考函数组合是很自然的。 That means the syntax for composition should be fun1 | fun2 | fun3 这意味着合成的语法应该是fun1 | fun2 | fun3 fun1 | fun2 | fun3 fun1 | fun2 | fun3 in a psudocode kind of way. fun1 | fun2 | fun3以一种psudocode的方式。 (where fun[i] is the i 'th function to apply in order). fun[i]i按顺序申请的功能)。 This order of functions is also what you find in haskell monadic binds. 这个函数的顺序也是你在haskell monadic绑定中找到的。 fun1 >>= fun2 >>= fun3 . fun1 >>= fun2 >>= fun3

But in other occasions in haskell, the order of functions is more math'y, such as fun3 . fun2 . fun1 但在haskell的其他场合,函数的顺序更像是数学,比如fun3 . fun2 . fun1 fun3 . fun2 . fun1 fun3 . fun2 . fun1 , or in the functorial setting fmap fun3 . fmap fun2 . fmap fun1 fun3 . fun2 . fun1 ,或在functorial设置fmap fun3 . fmap fun2 . fmap fun1 fmap fun3 . fmap fun2 . fmap fun1 fmap fun3 . fmap fun2 . fmap fun1 . fmap fun3 . fmap fun2 . fmap fun1

I am very much aware that the functions have different signature in the two examples, but it puzzles me that the structure is reversed, still. 我非常清楚这些函数在两个例子中有不同的签名,但令我感到困惑的是,结构仍然是相反的。 My workaround is to sometimes define a function mmap = flip (>>=) such that I can write mmap fun3 . mmap fun2 . mmap fun1 我的解决方法是有时定义一个函数mmap = flip (>>=)这样我就可以编写mmap fun3 . mmap fun2 . mmap fun1 mmap fun3 . mmap fun2 . mmap fun1 mmap fun3 . mmap fun2 . mmap fun1 . mmap fun3 . mmap fun2 . mmap fun1

So to the question: 那么问题是:

  1. Is there a mmap already defined? 是否已经定义了mmap What is is called? 叫什么叫?
  2. Why if bind defined as an operator with arguments in an order that feels backwards? 为什么绑定定义为具有参数的运算符,其顺序感觉倒退?

Is there a mmap already defined? 是否已经定义了mmap What is is called? 叫什么叫?

Hoogle is your friend. Hoogle是你的朋友。 The type signature of (>>=) is: (>>=)的类型签名是:

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

Hence, you're looking for a function with the type signature: 因此,您正在寻找具有类型签名的函数:

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

This is in fact the =<< function. 这实际上是=<<函数。 Hence, you can write fun3 =<< fun2 =<< fun1 . 因此,你可以写fun3 =<< fun2 =<< fun1

Why is bind defined as an operator with arguments in an order that feels backwards? 为什么绑定被定义为具有参数的运算符,其顺序感觉倒退?

It's because monadic code looks a lot like imperative code. 这是因为monadic代码看起来很像命令式代码。 For example, consider the following: 例如,请考虑以下事项:

permute2 :: [a] -> [[a]]
permute2 xs = do
    x  <- xs
    xs <- map return xs
    return (x:xs)

Without the syntactic sugar of do it would be written as: 如果没有的语法糖do会被写成:

permute2 :: [a] -> [[a]]
permute2 xs =
    xs >>= \x ->
    map return xs >>= \xs ->
    return (x:xs)

See the similarity? 看到相似度? If we used =<< instead it would look like: 如果我们使用=<<而不是它看起来像:

permute2 :: [a] -> [[a]]
permute2 xs = (\x -> (\xs -> return (x:xs)) =<< map return xs) =<< xs

Not very readable is it? 不是很可读吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM