适用于List`dist`功能的分销商 [英] Applicative Distributor for List `dist` Function

查看:114
本文介绍了适用于List`dist`功能的分销商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我从 dist 函数。 htmlrel =nofollow>使用效果编程

本文在前面加上这个函数:


你是否注意到序列和转置现在看起来相似?

编译器从它们的类型中推断出区分这两个程序的细节。两者都是应用型
分销商的实例



  dist :: Applicative f => [f a]  - > f [a] 
dist [] = [[]]
dist(v:vs)= [(:) v(dist vs)]
pre>

但是,我得到以下编译时错误:

  ghci中> :l ApplicativePaper.hs 
[1的1]编译Main(ApplicativePaper.hs,解释)

ApplicativePaper.hs:12:20:
无法推导出(a〜fa )
来自上下文(Applicative f)
由类型签名绑定
dist :: Applicative f => [f a] - > f [a]
在ApplicativePaper.hs:10:9-39
`a'是一个刚性类型变量,由
绑定dist :: Applicative f =>的类型签名。 [f a] - > f [a]
在ApplicativePaper.hs:10:9
相关绑定包括
vs :: [fa](绑定在ApplicativePaper.hs:12:9)
v :: fa(bound at ApplicativePaper.hs:12:7)
dist :: [fa] - >在[(:)的第一个参数中,即'v'
在表达式中:(:) v(dist vs)(在ApplicativePaper.hs:11:
失败,模块加载:无。

请让我知道我做错了什么。另外,请为此功能提供直觉。

解决方案

在本文中,双括号 [f u1 ... un = 在第4页定义为与纯f <*>相同。 u1 * ...< *> un 。



$ dist 在文本中的定义是

  dist ::应用f⇒[fa]→f [a] 
dist [] = [[] =
dist (v:vs)= [(:) v(dist vs)=

将其他符号翻译成Haskell并取代 [... = 的定义导致

  dist :: Applicative f => [f a]  - > f [a] 
dist [] =纯[]
dist(v:vs)=纯(:) * v * (dist vs)


I think that I copied the following dist function from Applicative Programming with Effects:

The paper prefaces this function with:

Have you noticed that sequence and transpose now look rather alike? The details that distinguish the two programs are inferred by the compiler from their types. Both are instances of the applicative distributor for lists

dist :: Applicative f => [f a] -> f [a]
dist []       = [[]]
dist (v : vs) = [(:) v (dist vs)]

However, I get the following compile-time error:

ghci> :l ApplicativePaper.hs
[1 of 1] Compiling Main             ( ApplicativePaper.hs, interpreted )

ApplicativePaper.hs:12:20:
    Could not deduce (a ~ f a)
    from the context (Applicative f)
      bound by the type signature for
                 dist :: Applicative f => [f a] -> f [a]
      at ApplicativePaper.hs:10:9-39
      `a' is a rigid type variable bound by
          the type signature for dist :: Applicative f => [f a] -> f [a]
          at ApplicativePaper.hs:10:9
    Relevant bindings include
      vs :: [f a] (bound at ApplicativePaper.hs:12:9)
      v :: f a (bound at ApplicativePaper.hs:12:7)
      dist :: [f a] -> f [a] (bound at ApplicativePaper.hs:11:1)
    In the first argument of `(:)', namely `v'
    In the expression: (:) v (dist vs)
Failed, modules loaded: none.

Please let me know what I'm doing wrong. Also, please provide an intuition for this function.

解决方案

In the paper, the double bracket 〚 f u1 … un 〛 is defined on page 4 to be the same as pure f <*> u1 <*> … <*> un.

The definition of dist in the text is

dist :: Applicative f ⇒ [f a] → f [a ]
dist []       = 〚 [] 〛
dist (v : vs) = 〚 (:) v (dist vs) 〛

Translating the other symbols into Haskell and making the substitution for the definition of 〚 … 〛 results in

dist :: Applicative f => [f a] -> f [a ]
dist []       = pure []
dist (v : vs) = pure (:) <*> v <*> (dist vs)

这篇关于适用于List`dist`功能的分销商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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