應用型分銷商,我認爲,我從Applicative Programming with Effects複製以下dist
功能:的列表`dist`功能
本文前言此函數:
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)]
不過,我得到以下編譯時錯誤:
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.
請讓我知道我在做什麼NG。另外,請提供此功能的直覺。
或簡單地'dist(v:vs)=(:) <$> v <*> dist vs'。 – 2014-12-08 04:26:21
@AaditMShah雖然這是正確的,但通過「純」的定義更加一致:用「純」提升「列表」的第一個元素,然後用「<*>」加入所有內容。這樣''[[] ='有理由'純粹[]'。 – 2014-12-08 11:35:10