Avatar of guygastineau

guygastineau's solution

to Bob in the Scheme Track

Published at Sep 19 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, 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.

Track Specific Notes

For extra fun see if you can clearly separate responsibilities in your code. Using symbol messages can help by acting like enums or ADTs in other languages.

Running and testing your solutions


  • Start a REPL either in your favorite editor or from the command line.
  • Type (load "bob.scm") at the prompt.
  • Test your code by calling (test) from the REPL.
  • Develop the solution in bob.scm and reload as you go.

Testing options

You can see more or less information about failing test cases an by passing additional arguments to the procedure test. To see the failing input call (test 'input) and to see the input and output together call (test 'input 'output).


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.



(import (rnrs))

(define test-fields '(input output who))

(define (test-run-solution solution input)
  (if (procedure? solution) (apply solution input) solution))

(define (test-success description success-predicate
         procedure input output)
    (lambda (k)
        (lambda (e)
          (k `(fail
                (description . ,description)
                (input . ,input)
                (output . ,output)
                (who . ,procedure))))
        (lambda ()
          (let ([result (test-run-solution procedure input)])
            (unless (success-predicate result output)
              (error 'exercism-test
                "test fails"
          `(pass . ,description))))))

(define (test-error description procedure input)
    (lambda (k)
        (lambda (e) (k `(pass . ,description)))
        (lambda ()
          (test-run-solution procedure input)
             (description . ,description)
             (input . ,input)
             (output . error)
             (who . ,procedure)))))))

(define (run-test-suite tests . query)
    (lambda (field)
      (unless (and (symbol? field) (memq field test-fields))
        (error 'run-test-suite
          (format "~a not in ~a" field test-fields))))
  (let-values ([(passes failures)
                  (lambda (result) (eq? 'pass (car result)))
                  (map (lambda (test) (test)) tests))])
      [(null? failures) (format #t "~%Well done!~%~%") 'success]
         "~%Passed ~a/~a tests.~%~%The following test cases failed:~%~%"
         (length passes)
         (length tests))
         (lambda (failure)
             "* ~a~%"
               [(assoc 'description (cdr failure)) => cdr]
               [else (cdr failure)]))
             (lambda (field)
               (let ([info (assoc field (cdr failure))])
                 (format #t "  - ~a: ~a~%" (car info) (cdr info))))

(define (test . args)
     (lambda ()
       (test-success "stating something" equal? response-for
         '("Tom-ay-to, tom-aaaah-to.") "Whatever."))
     (lambda ()
       (test-success "shouting" equal? response-for '("WATCH OUT!")
         "Whoa, chill out!"))
     (lambda ()
       (test-success "shouting gibberish" equal? response-for
         '("FCECDFCAAB") "Whoa, chill out!"))
     (lambda ()
       (test-success "asking a question" equal? response-for
         '("Does this cryogenic chamber make me look fat?") "Sure."))
     (lambda ()
       (test-success "asking a numeric question" equal?
         response-for '("You are, what, like 15?") "Sure."))
     (lambda ()
       (test-success "asking gibberish" equal? response-for
         '("fffbbcbeab?") "Sure."))
     (lambda ()
       (test-success "talking forcefully" equal? response-for
         '("Let's go make out behind the gym!") "Whatever."))
     (lambda ()
       (test-success "using acronyms in regular speech" equal? response-for
         '("It's OK if you don't want to go to the DMV.")
     (lambda ()
       (test-success "forceful question" equal? response-for
         "Calm down, I know what I'm doing!"))
     (lambda ()
       (test-success "shouting numbers" equal? response-for
         '("1, 2, 3 GO!") "Whoa, chill out!"))
     (lambda ()
       (test-success "no letters" equal? response-for '("1, 2, 3")
     (lambda ()
       (test-success "question with no letters" equal? response-for
         '("4?") "Sure."))
     (lambda ()
       (test-success "shouting with special characters" equal? response-for
         '("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")
         "Whoa, chill out!"))
     (lambda ()
       (test-success "shouting with no exclamation mark" equal?
         response-for '("I HATE THE DMV") "Whoa, chill out!"))
     (lambda ()
       (test-success "statement containing question mark" equal? response-for
         '("Ending with ? means a question.") "Whatever."))
     (lambda ()
       (test-success "non-letters with question" equal?
         response-for '(":) ?") "Sure."))
     (lambda ()
       (test-success "prattling on" equal? response-for
         '("Wait! Hang on. Are you going to be OK?") "Sure."))
     (lambda ()
       (test-success "silence" equal? response-for '("")
         "Fine. Be that way!"))
     (lambda ()
       (test-success "prolonged silence" equal? response-for
         '("          ") "Fine. Be that way!"))
     (lambda ()
       (test-success "alternate silence" equal? response-for
         '("\t\t\t\t\t\t\t\t\t\t") "Fine. Be that way!"))
     (lambda ()
       (test-success "multiple line question" equal? response-for
         '("\nDoes this cryogenic chamber make me look fat?\nNo.")
     (lambda ()
       (test-success "starting with whitespace" equal? response-for
         '("         hmmmmmmm...") "Whatever."))
     (lambda ()
       (test-success "ending with whitespace" equal? response-for
         '("Okay if like my  spacebar  quite a bit?   ") "Sure."))
     (lambda ()
       (test-success "other whitespace" equal? response-for
         '("\n\r \t") "Fine. Be that way!"))
     (lambda ()
       (test-success "non-question ending with whitespace" equal? response-for
         '("This is a statement ending with whitespace      ")
(import (rnrs (6)))

(load "test.scm")

(define (response-for message)
  (bob-reply (string-trim-both message)))

;; Now we can really easily write new responder functions
(define (bob-reply message)
  (case (classify message)
   ((Silence)   "Fine. Be that way!")
   ((Statement) "Whatever.")
   ((Yelling)   "Whoa, chill out!")
   ((Question)  "Sure.")
   ((YellQuest) "Calm down, I know what I'm doing!")))

(define classify
  (let ((yelling?
         (λ (message)
           (and (string-any char-upper-case? message)
                (not (string-any char-lower-case? message)))))
         (λ (message)
           (unless (string-null? message)
             (char=? #\? (string-last message))))))
    (lambda (message)
      (define question (question? message))
      (define yelling  (yelling?  message))
      ;; transform results to enum
      (cond ((string-null? message) 'Silence)
            ((and yelling question) 'YellQuest)
            (yelling                'Yelling)
            (question               'Question)
            (else                   'Statement)))))

;; Unsafe Last
(define (string-last string)
  (string-ref string (1- (string-length string))))

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?