Avatar of damienjakob

damienjakob's solution

to Bob in the Common Lisp Track

Published at Dec 24 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.


Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.


While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.


Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.


Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.


Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


(ql:quickload "lisp-unit")
#-xlisp-test (load "bob")

(defpackage #:bob-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:bob-test)

(define-test responds-to-something
  (assert-equal "Whatever." (bob:response-for "Tom-ay-to, tom-aaaah-to.")))

(define-test responds-to-shouts
  (assert-equal "Whoa, chill out!" (bob:response-for "WATCH OUT!")))

(define-test shouting-gibberish
  (assert-equal "Whoa, chill out!" (bob:response-for "FCECDFCAAB")))

(define-test responds-to-questions
  (assert-equal "Sure." (bob:response-for "Does this cryogenic chamber make me look fat?")))

(define-test asking-a-numeric-question
  (assert-equal "Sure." (bob:response-for "You are, what, like 15?")))

(define-test asking-gibberish
  (assert-equal "Sure." (bob:response-for "fffbbcbeab?")))

(define-test responds-to-forceful-talking
  (assert-equal "Whatever." (bob:response-for "Let's go make out behind the gym!")))

(define-test responds-to-acronyms
  (assert-equal "Whatever." (bob:response-for "It's OK if you don't want to go to the DMV.")))

(define-test responds-to-forceful-questions
  (assert-equal "Calm down, I know what I'm doing!"
                (bob:response-for "WHAT THE HELL WERE YOU THINKING?")))

(define-test responds-to-shouting-numbers
  (assert-equal "Whoa, chill out!" (bob:response-for "1, 2, 3 GO!")))

(define-test responds-to-only-numbers
  (assert-equal "Whatever." (bob:response-for "1, 2, 3")))

(define-test responds-to-number-question
  (assert-equal "Sure." (bob:response-for "4?")))

(define-test responds-to-shouting-with-special-characters
  (assert-equal "Whoa, chill out!" (bob:response-for "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")))

(define-test responds-to-shouting-with-no-exclamation-mark
  (assert-equal "Whoa, chill out!" (bob:response-for "I HATE YOU")))

(define-test responds-to-statement-containing-question-mark
  (assert-equal "Whatever." (bob:response-for "Ending with ? means a question.")))

(define-test non-letters-with-question
  (assert-equal "Sure." (bob:response-for ":) ?")))

(define-test prattling-ton
  (assert-equal "Sure." (bob:response-for "Wait! Hang on. Are you going to be OK?")))

(define-test responds-to-silence
  (assert-equal "Fine. Be that way!" (bob:response-for "")))

(define-test responds-to-prolonged-silence
  (assert-equal "Fine. Be that way!" (bob:response-for "    ")))

(define-test responds-to-alternate-silence
  (assert-equal "Fine. Be that way!"
                (bob:response-for (coerce (list #\Tab #\Tab #\Tab #\Tab) 'string))))

(define-test respond-to-multiline-question
  (assert-equal "Whatever."
                (bob:response-for (format nil "~%Does this cryogenic chamber make me look fat?~%no"))))

(define-test starting-with-whitespace
  (assert-equal "Whatever." (bob:response-for "         hmmmmmmm...")))

(define-test ending-with-whitespace
  (assert-equal "Sure." (bob:response-for "Okay if like my  spacebar  quite a bit?   ")))

(define-test other-whitespace
  (assert-equal "Fine. Be that way!"
                (bob:response-for (coerce (list #\Newline #\Tab #\Page) 'string))))

(define-test non-question-ending-with-whitespace
  (assert-equal "Whatever." (bob:response-for "This is a statement ending with whitespace      ")))

(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all))
(in-package #:cl-user)
(defpackage #:bob
  (:use #:cl)
  (:export #:response-for))
(in-package #:bob)

;; list of all (most common) whitespaces
(defvar whitespaces
  '(#\Space #\Newline #\Backspace #\Tab #\Linefeed #\Page #\Return #\Rubout))

(defun trim-whitespaces (sentence)
  "Remove whitespaces at the beginning and end of the sentence"
  (string-trim whitespaces sentence))

(defun is-empty (string)
  "Test if a string is empty"
  (string-equal "" string))

(defun last-char (string)
  "Get the last char of a string"
  (let ((string-length (length string)))
    ;; We can't get the last char of an empty string
    (unless (is-empty string)
      (aref string (1- string-length)))))

(defun does-not-say-something (sentence)
  "Test if the sentence contains at least one non-whitespace char"
  (is-empty (trim-whitespaces sentence)))

(defun is-question (sentence)
  "Test if the sentence is a question"
  ;; Test if the sentence ends by ? (not counting whitespace)
  ;; We use eql and not char= to allow last-char to return nil in case of empty string
  (eql #\? (last-char (trim-whitespaces sentence))))

(defun is-yelling (sentence)
  "Test if the sentence has been yelled"
  ;; Sentence must be in uppercase
  ;; and contain at least one char that is different between uppercase and lowercase 
  (and (string= sentence (string-upcase sentence))
       (string/= sentence (string-downcase sentence))))

(defun response-for (input)
  "Return Bob's response for the input"
  (let ((is-yelling (is-yelling input))
        (is-question (is-question input)))
      ((and is-yelling is-question) "Calm down, I know what I'm doing!")
      (is-yelling "Whoa, chill out!")
      (is-question "Sure.")
      ((does-not-say-something input) "Fine. Be that way!")
      ;; default
      (t "Whatever."))))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?