我刚开始用 Racket 编程,现在我遇到了以下问题。我有一个带有列表的结构,我必须将列表中的所有价格加起来。
(define-struct item (name category price))
(define some-items
(list
(make-item "Book1" 'Book 40.97)
(make-item "Book2" 'Book 5.99)
(make-item "Book3" 'Book 20.60)
(make-item "Item" 'KitchenAccessory 2669.90)))
我知道我可以用:(item-price (first some-items))
或返回价格(item-price (car some-items))
。
问题是,我不知道如何将所有商品的价格加起来。
对Óscar López的回答:我可能没有正确填写空白,但是当我按下开始时,Racket将代码标记为黑色并且不返回任何内容。
(define (add-prices items)
(if (null? items)
0
(+ (first items)
(add-prices (rest items)))))