Skip to content

Latest commit

 

History

History
155 lines (122 loc) · 4.33 KB

index.org

File metadata and controls

155 lines (122 loc) · 4.33 KB

Exercism.io → Clojure → Bob

Bob

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.

Source

Inspired by the ‘Deaf Grandma’ exercise in Chris Pine’s Learn to Program tutorial.

Solution

(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>> ...)

cond

TODO: discuss cond

Did you even say anything?

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)

ARE YOU YELLING‽

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))

What’s that?

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.

Whatever

If none of the tests above hold, Bob responds, Whatever.

:else "Whatever."

All together now

Finally, we end up with the following definition of response-for:

(<<response-for_signature>>
<<response-for_body>>)

Tests

lein test bob
Ran 14 tests containing 14 assertions.
0 failures, 0 errors.