1. 程式人生 > >函式程式設計實驗五:列印購物小票

函式程式設計實驗五:列印購物小票

{-
吳坎
17341163
[email protected]
資料科學與計算機學院計算機專業
-}
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
-}