这是一种更新手的方法,也可以完成这项工作。可能在优雅等级上得分为负,但非常简单。
如果您正在编写自己的内容,请不要在下面偷看。我敢打赌它比这短:-)
您应该能够调用 ghci 中的每个组件函数来将其拆开。
type Product = String
type Count = Float -- type chosen for simple arithmetic
type Price = Float
type Bill = [(Count, Product, Price)]
type LinePrice = (Product, Price)
myBill :: Bill
myBill = [ (4.0, "Bananas", 0.30), (1.0, "Apple", 0.50), (2.0, "Eggs", 0.25) ]
priceList :: Bill -> [LinePrice]
priceList = map (\(ct,prd,prc) -> (prd, ct*prc))
maxItem :: [LinePrice] -> LinePrice
maxItem lst = maxItem_m ("none",0.0) lst
maxItem_m :: LinePrice -> [LinePrice] -> LinePrice
maxItem_m max [] = max
maxItem_m (prd_m,prc_m) ((prd,prc):rest)
| prc > prc_m = maxItem_m (prd,prc) rest
| otherwise = maxItem_m (prd_m,prc_m) rest
main = do
let mostExpensive = maxItem $ priceList myBill
print mostExpensive