magic-haskell icon indicating copy to clipboard operation
magic-haskell copied to clipboard

p136 (<$)

Open boboyada opened this issue 8 years ago • 3 comments

(<$)=fmap . const 与 (<$)=fmap const 有什么区别? 1.前者是中间有个复合函数运算符 2.后者直接把const当作f函数,将f函数提升到函子盒子级别

boboyada avatar Jan 05 '17 23:01 boboyada

看来你已经自问自答了😄 所以你的问题是? 另外(<$)=fmap const是出自哪里的定义?

winterland1989 avatar Jan 06 '17 02:01 winterland1989

magic-haskell ,P136页, (<$) :: Functor f => a -> f b -> fa (<$) = fmap (.) const

($>) ::Functor f => f a -> b -> f b ($>) = flip (<$)

这都是书上的定义呀。 我想询问的是 fmap (。) const fmap const 这两个表达式有什么区别?

boboyada avatar Jan 06 '17 06:01 boboyada

(<$)=fmap const

并不是出自书上的定义,我也没有在别的地方看到有定义这样的(<$)运算符。

1.前者是中间有个复合函数运算符 2.后者直接把const当作f函数,将f函数提升到函子盒子级别

这两个表达式的区别你不是说得挺清楚了嘛,两个表达式的含义很不同呀:

fmap (.) const == \ x y -> fmap (const x) y         -- 组合函数,先应用const,再应用fmap
fmap const == \ x -> fmap const x                      -- 部分应用,直接应用fmap

winterland1989 avatar Jan 06 '17 07:01 winterland1989