formatBill :: BillType -> String
formatBill bill = merge' [pname ++ dots ++ show pprice | (pname, pprice) <- bill]
where
dots = ['.' | x<-[1..(lineLength - length pname - length (show pprice))]]
Run Code Online (Sandbox Code Playgroud)
这是我的代码 - formatBill是一个必须返回的函数String.
它应该返回的一个例子:
Product name.................. PRICE
Other product................. 4555
Run Code Online (Sandbox Code Playgroud)
merge' 只是 [String] -> String
type BillType = [(String, Int)] -- BillType definition
lineLength = 30 -- length of row
Run Code Online (Sandbox Code Playgroud)
这些是我得到的错误:
code.hs:69:51:不在范围内:`pname'
code.hs:69:72:不在范围内:`pprice'
一个where条款作用域在整个函数定义,所以你不能使用的东西,只有在列表中的理解范围.
要么dots将它们作为参数制作成函数:
formatBill :: BillType -> String
formatBill bill =
merge' [pname ++ dots pname pprice ++ show pprice | (pname, pprice) <- bill]
where
dots pname pprice =
['.' | x<-[1..(lineLength - length pname - length (show pprice))]]
Run Code Online (Sandbox Code Playgroud)
或者使用let列表内部理解:
formatBill :: BillType -> String
formatBill bill =
merge' [pname ++ dots ++ show pprice
| (pname, pprice) <- bill
, let dots = ['.' | x <- [1..(lineLength
- length pname
- length (show pprice))]]]
Run Code Online (Sandbox Code Playgroud)