Avatar of tvirolai

tvirolai's solution

to Bob in the Common Lisp Track

Published at Jan 07 2020 · 0 comments
Instructions
Test suite
Solution

Note:

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.

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

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

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

(defun questionp (input)
  (when (> (length input) 0)
    (let ((last-char (char input (1- (length input)))))
      (when last-char
        (char= #\? last-char)))))

(defun yellp (input)
  (and
   (string/= (string-upcase input) (string-downcase input))
   (string= input (string-upcase input))))

(defun silencep (input)
  (string= "" input))

(defun response-for (input)
  (let ((normalized-input (string-trim '(#\Space #\Tab #\Newline #\Page) input)))
    (cond
      ((and (questionp normalized-input)
            (yellp normalized-input)) "Calm down, I know what I'm doing!")
      ((questionp normalized-input) "Sure.")
      ((yellp normalized-input) "Whoa, chill out!")
      ((silencep normalized-input) "Fine. Be that way!")
      (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?