formatBill :: BillType -> String
formatBill bill = merge' [pname ++ dots ++ show pprice | (pname, pprice) <- bill]
where
dots = ['.' | x<-[1..(lineLength - length pname - length (show pprice))]]
这是我的代码 - formatBill
是一个必须返回String
的函数。
它应该返回的一个例子:
Product name.................. PRICE
Other product................. 4555
merge'
只是[String] -> String
type BillType = [(String, Int)] -- BillType definition
lineLength = 30 -- length of row
这些是我得到的错误:
code.hs:69:51:不在范围内:`pname'
code.hs:69:72:不在范围内:`pprice'
答案 0 :(得分:5)
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))]]
或者在列表理解中使用let
:
formatBill :: BillType -> String
formatBill bill =
merge' [pname ++ dots ++ show pprice
| (pname, pprice) <- bill
, let dots = ['.' | x <- [1..(lineLength
- length pname
- length (show pprice))]]]