Bob is a lackadaisical teenager. In conversation, his responses are very limited.
Bob answers ‘Sure.’ if you ask him a question.
He answers ‘Whoa, chill out!’ if you yell at him.
He says ‘Fine. Be that way!’ if you address him without actually saying anything.
He answers ‘Whatever.’ to anything else.
Inspired by the ‘Deaf Grandma’ exercise in Chris Pine’s Learn to Program tutorial.
(ns bob
"Mimicking the conversational ineptitude of a lackadaisical teenager."
{:author "Eric Bailey"})
Given a prompt, response-for
returns Bob’s lackadaisical response.
(<<response-for_signature>> ...)
TODO: discuss cond
According to the specification, if you don’t even say anything to Bob, the
response should be Fine. Be that way!
To check if prompt
consists entirely of whitespace, we can use ~re-matches~
with the regular expression ^\s*$
, i.e. match if it starts with zero or more
whitespace characters and then ends, with nothing else between.
(re-matches #"^\s*$" prompt)
If the prompt is yelled, Bob should respond with Whoa, chill out!
To determine whether prompt
is yelled, we first need to check if it contains
at least one uppercase alphabetic character:
(re-find #"[A-Z]" prompt)
and if so, check if prompt
is equal to itself with every character uppercased:
(= prompt (clojure.string/upper-case prompt))
If you ask Bob a question, he invariably answers, Sure.
i.e. if prompt
ends
with ?
.
The code to check very closely resembles English.
(.endsWith prompt "?")
We could modify the syntax a bit to get even more English-looking:
(. prompt (endsWith "?"))
As a matter of taste, I prefer the former.
If none of the tests above hold, Bob responds, Whatever.
:else "Whatever."
Finally, we end up with the following definition of response-for
:
(<<response-for_signature>>
<<response-for_body>>)
lein test bob
Ran 14 tests containing 14 assertions. 0 failures, 0 errors.