;;---------- Vending machine example - final version -------------------- (define soda-cost 50) (define candy-cost 65) ;; amt: Natural ;; remembers the amount of money in the vending machine (define amt 0) ;; insert-coin: Natural -> void ;; inserts the given amount of cents into the machine ;; EFFECT: changes amt (define (insert-coin cents) (set! amt (+ amt cents))) ;; choose-item: String -> String ;; consumes the item the user wants and produces a status message ;; EFFECT: can change the amt in the vending machine (define (choose-item item) (cond [(string=? item "soda") (if (>= amt soda-cost) (local [(define change (- amt soda-cost))] (begin (set! amt 0) (format "dispense soda and ~a cents change" change) )) (format "need ~a more cents" (- soda-cost amt)))] [(string=? item "candy") (if (>= amt candy-cost) "dispense candy" "need more money")]))