I'm trying to write a program where a doctor sees only 5 patients and then the program ends. Right now it's not ending and it keeps asking for the next patient. Ignore the else in the doctor-driver-loop procedure. I add one to count in that procedure but I guess it keeps going back to zero every time. How do I fix this?
(define count 0)
(define (new-patient counter)
(if (= counter 5) (write-line 'doctor has seen 5 patients today so the day is now over)
(visit-doctor (ask-patient-name))))
(define (doctor-driver-loop name earlier-responses)
(newline)
(write '**)
(let ((user-response (read)))
(cond ((equal? user-response '(goodbye))
(write-line (list 'goodbye name))
(write-line '(see you next week))
(new-patient (+ 1 count)))
(else (write-line (reply (responses-list earlier-responses user-response) user-response))
(doctor-driver-loop name (responses-list earlier-responses user-response))))))
(define (visit-doctor name)
(write-line (list 'hello name))
(write-line '(what seems to be the trouble?))
(doctor-driver-loop name initial-earlier-response))
Try replacing this line:
(new-patient (+ 1 count))
With these two lines:
(set! count (+ 1 count))
(new-patient count)
In your current code, count
will always have a value of zero for each iteration of doctor-driver-loop
, because its value was never updated - the (+ 1 count)
part adds one to count
without changing count
, and the next time doctor-driver-loop
get called, count
will be zero again.
Be aware that this is a quick fix, but not the ideal solution. For starters, count
should not be defined as a global variable, instead it should be a parameter to the driver loop that gets passed with an initial value of zero and incremented when calling new-patient
with each patient that gets processed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.