通过利用高阶函数,实现该函数的最佳方法是makeCloths
什么?我希望实现的是makeCloths
可以methodsList
使用materialList
. 这样以后如果在 中添加更多的方法methodsList
,并且这些方法只使用 中的参数materialList
,我们就不需要修改 中的代码makeCloths
。
data Material = WhiteCloth Int
| BlueCloth Int
| RedCloth Int
makeWhiteShirt :: Material -> Clothe
makeWhiteShirt (WhiteCloth x) = .....
makeBluePants :: Material -> Clothe
makeBluePants (BlueCloth x) = .....
makeRedBlueDress :: Material -> Material -> Clothe
makeRedBlueDress (RedCloth x) (BlueCloth y) = ......
methodsList = [ makeWhiteShirt, makeBluePants, makeRedBlueDress ]
materialList = [ WhiteCloth 3, BlueCloth 2, RedCloth 2]
-- call makeCloths like so
-- listOfClothes = makeCloths methodsList materialList
makeCloths :: [a] -> [b] -> [Clothe]