(defun count-valleys (n &optional (altitude 0) (total 0)) (if (= n 0) total (let ((c (read-char))) (if (eql c #\U) (count-valleys (1- n) (1+ altitude) total) (if (= altitude 0) (count-valleys (1- n) -1 (1+ total)) (count-valleys (1- n) (1- altitude) total)))))) (format t "~d~%" (count-valleys (read)))