如何在Racket/Scheme中使用foldl/r添加两个数字的列表



我试图使用foldl/r添加两个"两个结构的列表",但无法添加这两个列表。我在下面附上了代码

(define-struct product-list (barcode expiry price ))
(define-struct tax-list (barcode tax))
(define (total-price products taxes)
(foldr 
+ (product-list-price products) (tax-list-tax taxes)))
(define products (list 
(make-product-list 1500 85)
(make-product-list 1501 30)
(make-product-list 1502 200)
(make-product-list 1503 15)
(make-product-list 1504 100)
))
(define taxes (list
(make-tax-list 1500 5)
(make-tax-list 1501 2)
(make-tax-list 1502 12)
(make-tax-list 1503 0)
(make-tax-list 1504 6)
))

假设:

  • 您想在两个输入列表中添加价格和税收元素
  • 产品列表中的产品已正确构建(在您的代码中,它们缺少条形码(

然后,您只需要从列表中提取价格和税款,并将它们相加,如下所示:

(define (total-price products taxes)
(foldr
+
0
(map product-list-price products)
(map tax-list-tax taxes)))

相关内容

最新更新