是不是正确的联想

是不是正确的联想

本文介绍了为什么f< $> g< $> x相当于(f。g)< $> x尽管< $>是不是正确的联想?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

为什么 f< $> g< $> x 相当于(f。g)< $> x 虽然< $> 不是正确的联想?



在中使用普通的 $ 时,有一种等同性是有效的,但是目前 $ 是右联合的!)

< *> code>具有与< $> 相同的关联性和优先级,但行为不同!



示例:

  Prelude Control.Applicative> (show。show)< $>只需3 
只需\3 \
Prelude Control.Applicative>显示< $>显示< $>只需3
只需\3 \
Prelude Control.Applicative>纯粹的节目*纯粹的节目*只有3

<互动>:12:6:
无法将'[Char]'类型与'a0 - > b0'
预期类型:(a1 - >字符串) - > a0 - > b0
实际类型:(a1 - >字符串) - >字符串
在'pure'的第一个参数中,即'show'
在'(<>)'的第一个参数中,即'pure show'
在第一个参数(*)',即'纯粹的节目*纯节目'
Prelude Control.Applicative>
Prelude Control.Applicative> :i(< $>)
(< $>):: Functor f => (a - > b) - > f a - > f b
- 在`Data.Functor'中定义
infixl 4< $>
Prelude Control.Applicative> :i(*)
class Functor f =>应用f其中
...
(*):: f(a - > b) - > f a - > f b
...
- 在`Control.Applicative'中定义
infixl 4 *
Prelude Control.Applicative>

< $> ,我期望显示< $>显示< $>

解决方案

与Haskell相比,这不是一个函子式的东西。它的工作原理是功能是仿函数。 < $> 操作符都可以在不同的函子中使用!

f <$ > g 实际上与 f相同。 g ,所以你所要求的等价性比 f <$> (g $ x)≡f。 g< $> x


Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?

(This kind of equivalence is valid in a popular idiom with plain $, but currently $ is right-associative!)

<*> has the same associativity and precedence as <$>, but behaves differently!

Example:

Prelude Control.Applicative> (show . show) <$> Just 3
Just "\"3\""
Prelude Control.Applicative> show <$> show <$> Just 3
Just "\"3\""
Prelude Control.Applicative> pure show <*> pure show <*> Just 3

<interactive>:12:6:
    Couldn't match type `[Char]' with `a0 -> b0'
    Expected type: (a1 -> String) -> a0 -> b0
      Actual type: (a1 -> String) -> String
    In the first argument of `pure', namely `show'
    In the first argument of `(<*>)', namely `pure show'
    In the first argument of `(<*>)', namely `pure show <*> pure show'
Prelude Control.Applicative>
Prelude Control.Applicative> :i (<$>)
(<$>) :: Functor f => (a -> b) -> f a -> f b
    -- Defined in `Data.Functor'
infixl 4 <$>
Prelude Control.Applicative> :i (<*>)
class Functor f => Applicative f where
  ...
  (<*>) :: f (a -> b) -> f a -> f b
  ...
    -- Defined in `Control.Applicative'
infixl 4 <*>
Prelude Control.Applicative>

From the definition of <$>, I would expect show <$> show <$> Just 3 to fail, too.

解决方案

This isn't so much a functor-thing as a Haskell-thing. The reason it works is that functions are functors. Both <$> operators work in different functors!

f <$> g is in fact the same as f . g, so the equivalence you're asking about is rather more trivial than f <$> (g <$> x) ≡ f . g <$> x.

这篇关于为什么f&lt; $&gt; g&lt; $&gt; x相当于(f。g)&lt; $&gt; x尽管&lt; $&gt;是不是正确的联想?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:43