An elevator starts on the ground floor
0 and moves up or down by one floor at a time as described by an input string
travel. Here's an example
u is a move up 1 floor and
d is a move down 1 floor.
count-basement-trips takes a
travel string and counts how many times the elevator returns back to the ground floor from the dark and mysterious underground levels.
(defun count-basement-trips (travel) (flet ((finished-basement-trip-p (old-floor new-floor) (and (= -1 old-floor) (= 0 new-floor))) (change (c) (if (char= c #\u) 1 -1))) (let ((floor 0) (count 0)) (loop for c across travel do (let ((new-floor (+ floor (change c)))) (if (finished-basement-trip-p floor new-floor) (incf count)) (setf floor new-floor))) count))) (count-basement-trips "udddudduuuuddu")
The last expression evaluates to
What changes would make this more idiomatic Common Lisp? My goal is to speak more like a native. Thanks!