我想在 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?
我想在 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?
我不会说这更简单,但你去:
maxmin :: Ord a => a -> a -> a -> a
maxmin = (. min) . (.) . max
pl
(使用来自lambdabot
http://www.haskell.org/haskellwiki/Pointfree的工具生成)
lambdabot> pl maxmin a b c = max a (min b c)
maxmin = (. min) . (.) . max
为此,您只需使用“三段定律”,
(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
这也不太可读。:)