;; ticketnum: Natural ;; remembers the last ticket number that was handed out (define ticketnum 4) ;; get-ticket: -> Natural ;; produces the next ticket number (define (get-ticket) (begin (if (= ticketnum 4) (set! ticketnum 1) (set! ticketnum (+ 1 ticketnum))) ticketnum)) (define-struct account (acctnum balance)) ;; an Account is a (make-account Natural Number) ;; where acctnum is the account number ;; balance is the balance in the account (in dollars) ;; a ListOfAccount is one of ;; empty ;; (cons Account ListOfAccount) ;; Citibank: ListOfAccount ;; remembers the information about each account in the list (define Citibank (list (make-account 1 500) (make-account 2 1000) (make-account 3 10))) ;; add-account: Natural Number -> void ;; makes a new account and adds it to Citibank ;; EFFECT: changes the contents of Citibank (define (add-account acctnum amt) (set! Citibank (cons (make-account acctnum amt) Citibank))) ;; remove-account: Natural -> void ;; removes the account with the given account number from Citibank ;; EFFECT: removes an account from Citibank (define (remove-account acctnum) (set! Citibank (remove-acct-list acctnum Citibank))) ;; remove-acct-list: Natural ListOfAccount -> ListOfAccount ;; removes the account with the given account number from the list (define (remove-acct-list acctnum aloa) (cond [(empty? aloa) (error "No such account")] [(cons? aloa) (if (= (account-acctnum (first aloa)) acctnum) (rest aloa) (cons (first aloa) (remove-acct-list acctnum (rest aloa))))])) "show Citibank (it contains an account with account number 2)" Citibank "run remove-account on Citibank, removing account 2" (remove-account 2) "show that Citibank no longer contains account 2, but all other accounts are still there" Citibank ;;---------- Vending machine example -------------------- ;; Here's the code we've developed so far. Make one more modification to the ;; vending machine that will tell you how much change you'll get back. For example, ;; if you insert 60 cents into the machine, then choose a soda, choose-item should ;; return a string that says "dispense soda and 10 cents change" (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) (begin (set! amt (- amt soda-cost)) "dispense soda") (format "need ~a more cents" (- soda-cost amt)))] [(string=? item "candy") (if (>= amt candy-cost) "dispense candy" "need more money")]))