Racket list in struct

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2012-11-07T17:26:12Z Indexed on 2012/11/07 23:00 UTC
Read the original article Hit count: 416

Filed under:
|
|
|
|

I just started programming with Racket and now I have the following problem. I have a struct with a list and I have to add up all prices in the list.

(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)))

I know that I can return the price with: (item-price (first some-items)) or (item-price (car some-items)).

The problem is, that I dont know how I can add up all Items prices with this.


Answer to Óscar López: May i filled the blanks not correctly, but Racket mark the code black when I press start and don't return anything.

(define (add-prices items)
  (if (null? items)           
             (+ 0 items)       ; Here I don't really know what to write for a 0. 
                               ; I tried differnt thnigs like null and this version.
      (+ (item-price (first some-items))               
        (add-prices (item-price (rest some-items))))))

© Stack Overflow or respective owner

Related posts about list

Related posts about homework