Avatar of mwcoomber

mwcoomber's solution

to Bob in the Common Lisp Track

Published at Feb 12 2020 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question, such as "How are you?".

He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).

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.

Setup

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

Formatting

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.

VIM

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

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.

Source

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.

bob-test.lisp

;;;
;;; bob v1.6.0
;;;
(ql:quickload "lisp-unit")
#-xlisp-test (load "bob")

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

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


(define-test
  shouting
  (assert-equal
    "Whoa, chill out!"
    (bob:response "WATCH OUT!")))


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


(define-test
  asking-a-question
  (assert-equal
    "Sure."
    (bob:response "Does this cryogenic chamber make me look fat?")))


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


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


(define-test
  talking-forcefully
  (assert-equal
    "Whatever."
    (bob:response "Hi there!")))


(define-test
  using-acronyms-in-regular-speech
  (assert-equal
    "Whatever."
    (bob:response "It's OK if you don't want to go work for NASA.")))


(define-test
  forceful-question
  (assert-equal
    "Calm down, I know what I'm doing!"
    (bob:response "WHAT'S GOING ON?")))


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


(define-test
  no-letters
  (assert-equal
    "Whatever."
    (bob:response "1, 2, 3")))


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


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


(define-test
  shouting-with-no-exclamation-mark
  (assert-equal
    "Whoa, chill out!"
    (bob:response "I HATE THE DENTIST")))


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


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


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


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


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


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


(define-test
  multiple-line-question
  (assert-equal
    "Whatever."
    (bob:response (format nil "~%
Does this cryogenic chamber make me look fat?~%
No."))))


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


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


(define-test
  other-whitespace
  (assert-equal
    "Fine. Be that way!"
    (bob:response (format nil "~%
        "))))


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

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

(defun response (str)
  (when (null str) (setf str ""))
  (setf str (string-trim '(#\Space #\Tab #\Newline) str))
  (if (string= "" str) "Fine. Be that way!"
    (let ((all-caps (and (some #'upper-case-p str) (notany #'lower-case-p str))))
      (if (char= #\? (elt str (1- (length str))))
        (if all-caps "Calm down, I know what I'm doing!" "Sure.")
        (if all-caps "Whoa, chill out!" "Whatever.")))))

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?