【发布时间】:2018-03-26 11:05:52
【问题描述】:
真的,我想找出更通用的解决方案,即在单个功能点中使用 zipwith 组合折叠。
zWMult :: Num c => [c] -> [c] -> [c]
zWMult = zipWith (*)
foldPl0 :: Num c => [c] -> c
foldPl0 = foldl (+) 0
当我使用争论时,我得到了正确的解决方案
dPr x y = foldPl0 (zWMult x y)
dPr x y = foldPl0 $ zWMult x y
但是不知道如何在没有参数的情况下自然地组合这些。这两个都失败了:
Prelude> :{
Prelude| let dPr1 :: Num c => [c] -> [c] -> c
Prelude| dPr1 = fPl0 $ zWMult
Prelude| :}
<interactive>:171:19:
Couldn't match expected type ‘[[c] -> [c] -> c]’
with actual type ‘[Integer] -> [Integer] -> [Integer]’
Relevant bindings include
dPr1 :: [c] -> [c] -> c (bound at <interactive>:171:5)
Probable cause: ‘zWMult’ is applied to too few arguments
In the second argument of ‘($)’, namely ‘zWMult’
In the expression: fPl0 $ zWMult
Prelude> :{
Prelude| let dPr1 :: Int c => [c] -> [c] -> c
Prelude| dPr1 = foldPl0 $ zWMult
Prelude| :}
<interactive>:11:13:
‘Int’ is applied to too many type arguments
In the type signature for ‘dPr1’: dPr1 :: Int c => [c] -> [c] -> c
还有
dPr2 = foldPl0 . zWMult
编辑: 很酷,如果您想更全面地了解下面的解决方案,应该交叉参考这篇文章。 What does (f .) . g mean in Haskell?
【问题讨论】:
-
您可以使用pointfree.io 转换为pointfree。小心使用,因为无点很容易变成混淆。