I’m getting a very weird error when testing my solution for the Bob exercise. This is the error message:
PROLONGED-SILENCE in BOB-SUITE []:
Unexpected Error: #<SB-INT:INVALID-ARRAY-INDEX-ERROR expected-type:
(INTEGER 0 (0))
datum: -1>
Invalid index -1 for (SIMPLE-ARRAY CHARACTER (0))..
This is my code:
(defun question-p (s)
(eq (aref s (1- (length s))) #\?))
(defun has-letter-p (s &optional (i 0))
(if (equal i (length s))
nil
(let ((char (aref s i)))
(if (alpha-char-p char)
t
(has-letter-p s (1+ i))))))
(defun yell-p (s &optional (i 0))
(if (equal i (length s))
t
(let ((char (aref s i)))
(if (and (alpha-char-p char) (lower-case-p char))
nil
(yell-p s (1+ i))))))
(defun silence-p (s &optional (i 0))
(labels ((whitespace-p (c) (member c '(#\Space #\Tab #\Newline))))
(if (equal i (length s))
t
(let ((c (aref s 0)))
(if (not (whitespace-p c))
nil
(silence-p s (1+ i)))))))
(defun response (hey-bob)
(let ((trimmed (string-trim " " hey-bob)))
(cond ((and (has-letter-p trimmed)
(yell-p trimmed)
(question-p trimmed)) "Calm down, I know what I'm doing!")
((question-p trimmed) "Sure.")
((and (has-letter-p trimmed) (yell-p trimmed)) "Whoa, chill out!")
((silence-p trimmed) "Fine. Be that way!")
(t "Whatever."))))
I can’t figure this one out. I can’t see any place in my code that even subtracts from the index I’m using. Any ideas?