我想在haskell中编写一个无点函数,为了简单起见,我想说要做这个函数:
maxmin :: Ord a => a -> a -> a -> a
maxmin a b c = max a (min b c)
我可以改进这个
maxmin a b = (max a) . (min b)
但有没有办法摆脱a和b?
答案 0 :(得分:8)
我不会说这更简单但是你走了:
maxmin :: Ord a => a -> a -> a -> a
maxmin = (. min) . (.) . max
(使用pl
http://www.haskell.org/haskellwiki/Pointfree中的lambdabot
工具生成)
lambdabot> pl maxmin a b c = max a (min b c)
maxmin = (. min) . (.) . max
答案 1 :(得分:3)
您只需使用"three laws of sections",
(a `op` b) = (a `op`) b = (`op` b) a = op a b
这样
import Control.Arrow
maxmin a b = (max a) . (min b)
= (.) (max a) (min b)
= uncurry (.) (max a, min b)
= uncurry (.) . (max *** min) $ (a, b)
= curry (uncurry (.) . (max *** min)) a b
也不太可读。 :)