函数程序设计实验五:打印购物小票 晓风の个人博客

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
-}