简体   繁体   English

fmap函数组成如何工作

[英]how does fmap function composition works

I am trying to understand how function composition works. 我试图了解功能组合的工作原理。

The common example is: 常见的示例是:

(.) :: (b -> c) -> (a -> b) -> a -> c  
f . g = \x -> f (g x)  

It is easy to understand, what a function composition is. 函数组成是什么很容易理解。

Now consider: 现在考虑:

(fmap . fmap) (+5) (Just (Just 4))

Let's consider first fmap implementation: 让我们考虑第一个fmap实现:

fmap . fmap = \x -> fmap ( fmap x )

How does the example is going to execute? 该示例如何执行?

Does fmap (+5) is going to execute first? fmap (+5)是否要首先执行?

What is the x value in connection of the example? 该示例的x值是多少?

(fmap . fmap) (+5) (Just (Just 4))
= { def. (.) }
fmap (fmap (+5)) (Just (Just 4))
= { fmap g (Just x) = Just (g x), where g = fmap (+5) ; x = Just 4 }
Just (fmap (+5) (Just 4))
= { fmap g (Just x) = Just (g x), where g = (+5) ; x = 4 }
Just (Just ((+5) 4))
= { def. (+) }
Just (Just 9)

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

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