Dot operator in haskell with multi-parameter functions

烂漫一生 提交于 2019-12-18 13:27:15

问题


I want to write a function point-free in haskell, to keep things simple lets say I want to make this function:

maxmin :: Ord a => a -> a -> a -> a
maxmin a b c = max a (min b c)

I can improve this to

maxmin a b = (max a) . (min b)

but is there any way to get rid of a and b?


回答1:


I wouldn't say this is simplier but here you go:

maxmin :: Ord a => a -> a -> a -> a                                             
maxmin = (. min) . (.) . max 

(Generated with pl tool from lambdabot http://www.haskell.org/haskellwiki/Pointfree)

lambdabot> pl maxmin a b c = max a (min b c)
maxmin = (. min) . (.) . max



回答2:


You just use the "three laws of sections" for that,

(a `op` b) = (a `op`) b = (`op` b) a = op a b

so that

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

which is not too readable either. :)



来源:https://stackoverflow.com/questions/13475042/dot-operator-in-haskell-with-multi-parameter-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!