如何组合一起返回Bool的函数
有没有办法将这些功能组合在一起?
data Patient = Patient
{ name :: Name,
sex :: Sex,
age :: Int,
height :: Int,
weight :: Int,
bloodType :: BloodType
}
canDonate :: BloodType -> BloodType -> Bool
canDonateTo :: Patient -> Patient -> Bool
目前我只是手动应用它们
canDonateTo :: Patient -> Patient -> Bool
canDonateTo x y = canDonate (bloodType x) (bloodType y)
但是,我想知道是否有更好的方法来做到这一点。
回答
使用Data.Function.on:
import Data.Function (on)
canDonateTo = canDonate `on` bloodType
(基本上,您的方法只是内联了 的定义on,可以定义为
on f g x y = f (g x) (g y)
)