CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
module HaskellStore where
import Text.Printf
type Items=[Item]
type Item=(Name,Amount,Price)
type Name=String
type Amount=Float
type Price=Float
printItems::Items->IO()
printItems its=putStr(printf "Name\tAmount\tPrice\tSum\n%sTotal\t\t\t%.2f\n" (printItem its) (sumItem its))
where
printItem []=""
printItem ((n,a,p):xs)=printf "%s\t%.2f\t%.2f\t%.2f\n%s" n a p (a*p) (printItem xs)
sumItem []=0
sumItem ((n,a,p):xs)=a*p+(sumItem xs)
{-
--测试代码
*HaskellStore> printItems [("Apple",2.5,5.99),("Bread",2,3.5)]
Name Amount Price Sum
Apple 2.50 5.99 14.97
Bread 2.00 3.50 7.00
Total 21.97
-}