需要知道什么是< *> < $>和。在哈斯克尔做 [英] need to know what <*> <$> and . do in haskell

查看:107
本文介绍了需要知道什么是< *> < $>和。在哈斯克尔做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (。)::(b  - > c) - > (a  - > b) - > a  - > c 
(< $>):: Functor f => (a - > b) - > f a - > f b
(*)::适用的f => f(a - > b) - > f a - > f b

可以吗?我没有任何想法,当我看到签名,perhabs一些简单和易于理解的解释的例子将帮助我。

我也在学习Haskell,我的建议是看看了解你对Haskell的好处!,更准确地说:



实质上:


  • (。)是函数组合:如果您有 g :: a - > b f :: b - > c 然后 f。 g 本质上是 f(g(x)):首先使用 g code> a 来得到一个 b ,然后使用 f code> b 得到 c


  • < $> 取一个函数,它取一个 a 并返回一个 b 和一个函数包含一个 a ,并且它返回一个函数包含一个 b 。因此< $> fmap ::(a - > b) - >相同。 f a - > fb


  • <> >包含一个函数,它带一个 a 并返回一个 b ,一个函数包含一个 a ,它返回一个包含 a b 的函子。所以<>< / code>< em>< em>>< em>进入仿函数




注意在本书章节中找到的解释优于我上面的尝试


hi guys can someone explain me as a haskell noob what the the operators:

(.) :: (b -> c) -> (a -> b) -> a -> c
(<$>) :: Functor f => (a -> b) -> f a -> f b
(<*>) :: Applicative f => f (a -> b) -> f a -> f b

do? i dont have any idea when i see the signatures, perhabs some example with a simple and easy to understand explanation will help me.

解决方案

I am also learning Haskell, and my recommendation is to have a look into Learn You a Haskell for Great Good!, and more precisely:

In essence:

  • (.) is function composition: if you have g :: a -> b and f :: b -> c then f . g is essentially f(g(x)): first use g on an a to get a b and then use f on that b to get a c

  • <$> takes a function taking an a and returning a b, and a functor that contains an a, and it returns a functor that contains a b. So <$> is the same as fmap :: (a -> b) -> f a -> f b

  • <*> takes a functor that contains a function taking an a and returning a b, and a functor that contains an a, and it returns a functor that contains a b. So <*> kind of extract the function from a functor and applies it to an arguments also inside a functor, and finally returns the result into a functor

Note the explanations that you find in the book chapters are better than my attempt above

这篇关于需要知道什么是&lt; *&gt; &LT; $&GT;和。在哈斯克尔做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆