Avatar of aj-4

aj-4's solution

to Bob in the Scheme Track

Published at Nov 04 2018 · 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 says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

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

;; Load SRFI-64 lightweight testing specification
(use-modules (srfi srfi-64))

;; Suppress log file output. To write logs, comment out the following line:
(module-define! (resolve-module '(srfi srfi-64)) 'test-log-to-file #f)

;; Require bob impl
(add-to-load-path (dirname (current-filename)))
(use-modules (bob))

;;; Begin test suite
(test-begin "bob-test")

(test-equal "responds-to-something"
            "Whatever."
            (response-for "To-may-to, tom-aaaah-to."))

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

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

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

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

(test-equal "responds-to-forceful-questions"
            "Whoa, chill out!"
            (response-for "WHAT THE HELL WERE YOU THINKING?"))

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

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

(test-equal "responds-to-shouting-wit-no-exclamation-mark"
            "Whoa, chill out!"
            (response-for "I HATE YOU"))

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

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

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

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

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

(test-end "bob-test")
;;; End test suite
(define-module (bob)
  #:export (response-for))

(define (response-for say)
  (define (is-yelling? last-word)
    (cond ((string-every (lambda (x) (not (char-alphabetic? x))) last-word) #f)
          ((equal? (string-upcase last-word) last-word) #t)
          (else #f)
    )
  )
  (let* ((a1 "Sure.")
         (a2 "Whoa, chill out!")
         (a3 "Fine. Be that way!")
         (a4 "Whatever.")

         (L (string-split say #\space))
         (lw (list-ref L (- (length L) 1)))
  )
    (cond ((string-null? say) a3)
          ((string-every #\space say) a3)
          ((eq? (string-ref lw (- (string-length lw) 1)) #\.) a4)
          ((is-yelling? lw) a2)
          ((eq? (string-ref say (- (string-length say) 1)) #\?) a1)
          (else a4)
    )))

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?