0

My assignment looks like this: Suppose you are writing a Scheme program for a grocery store that will calculate the total price of a customer’s items, represented as a list. Your partner suggests writing a series of Scheme forms like (define item price) then recursively going through the list using those defines to calculate the sum. I wrote the code, but when I call the function, I get an error.

#lang scheme
(define milk 2.99)
(define chicken 7.99)
(define carrots 1.99)
(define add-cart
  (lambda(x)
    (if (empty? x) 0
        (+ (car x) (add-cart (cdr x))))))
(add-cart '(milk chicken carrots))

Error:

+: contract violation
  expected: number?
  given: carrots

I am still new too scheme just got introduced to it last week, but it is really frustrating. What am I doing wrong? Maybe you have some advice? I would be grateful for any hint.

Barmar
  • 741,623
  • 53
  • 500
  • 612

0 Answers0