Exercism.io challenge - Bob

The Bob challenge involves writing a very basics example of a text parser, something that would be used for a text based adventure game.

Bob is described as a lackadaisical teenager, so responses are very limited. To create the Bob text parser we need to identify the rules that determine Bob's response.

The instructions provide some basic rules:

  • Bob answers 'Sure.' if you ask him a question.
  • He answers 'Whoa, chill out!' if you yell at him.
  • He answers 'Calm down, I know what I'm doing!' if you yell a question at him.
  • He says 'Fine. Be that way!' if you address him without actually saying anything.
  • He answers 'Whatever.' to anything else.

It is important to also read through the supplied unit tests to elaborate on these rules.

Create the project

Download the Bob transcription exercise using the exercism CLI tool

exercism download --exercise=bob --track=clojure

To use the Clojure CLI tool instead of Leiningen, create a deps.edn file containing an empty hash-map, {} and clone practicalli/clojure-deps-edn to ~/.clojure/.

Rules derived from the Unit tests

Reviewing all the examples from the unit tests, there are 5 rules for the Bob parser

These rules were discovered by searching through the unit test code for each reply that Bob should return, showing the tests for each reply.

Each rule also had to ensure it did not create any false positives by being true for any other reply that Bob could make, especially the whatever reply.

Name Rule description
question The phrase has a ? as the last alphanumeric character, not including whitespace
shouting The phrase has uppercase alphabetic characters, but no lower case alphabetic characters
shouting question A combination of question and shouting
silence The phrase is empty or contains characters that are not alphanumeric
whatever Any phrase that does not match any of the other rules

Design approach

There are two main approaches to solving this challenge. The first is to use the clojure.string functions to check or transform the phrase given to Bob. The second approach is to use regular expressions with functions such as re-seq, re-find and re-matches.

Start by defining the rules as an expression that returns either true or false, using some of the example strings from the unit tests.

Use a let expression to bind a name to each rule, e.g. shouting?, question?, scilence?. Then these names can be used in a simple cond expression to return the appropriate phrase. Regardless of if using clojure.string or regular expressions, the cond code should be simlar

Once you have tried this challenge for yourself, take a look at the design journal for the clojure.string approach and the regular expression approach.

results matching ""

    No results matching ""