有没有办法将这些函数组合在一起?
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)
)
关于haskell - 如何组合返回 Bool 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67621565/