偶尔我会偶然发现我想表达的问题,“请使用最后一个参数两次”,例如为了写无点式或避免lambda。例如。欺骗“重复使用”参数在Haskell中?
sqr x = x * x
可以写成
sqr = doubleArgs (*) where
doubleArgs f x = f x x
或者考虑这个稍微复杂功能(从this question拍摄):如果有类似的功能
ins x xs = zipWith (\ a b -> a ++ (x:b)) (inits xs) (tails xs)
我可以写这个代码pointfree此:
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的东西,所以我想我可能会在这里错过一个技巧或习惯用法。
而根据`pointfree`,`dup`可以算作'liftM2`。我真的需要更好地处理函数的monad实例。 – 2010-12-02 10:30:43
谢谢你们提供甚至**两种**方法来解决这些问题。顺便说一句我试过`sqr =(*)<$> id <*> id`,它也适用:-) – Landei 2010-12-02 10:57:34