有时我会偶然发现我想表达“请使用最后一个参数两次”的问题,例如为了编写无点样式或避免使用 lambda。例如
sqr x = x * x
可以写成
sqr = doubleArgs (*) where
doubleArgs f x = f x x
或者考虑这个稍微复杂一点的函数(取自这个问题):
ins x xs = zipWith (\ a b -> a ++ (x:b)) (inits xs) (tails xs)
如果有这样的函数,我可以无点编写这段代码:
ins x = dup (zipWith (\ a b -> a ++ (x:b))) inits tails where
dup f f1 f2 x = f (f1 x) (f2 x)
但是因为我在 Hoogle 中找不到类似 doubleArgs 或 dup 的东西,所以我想我可能会在这里错过一个技巧或成语。