Avatar of aecepoglu

aecepoglu's solution

to Bob in the Scheme Track

Published at Oct 27 2019 · 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, 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

See if you can clearly separate responsibilities in your code.

Running and testing your solutions

From the command line

Simply type make chez if you're using ChezScheme or make guile if you're using GNU Guile.

From a REPL

  • Enter test.scm at the repl prompt.
  • Develop your solution in bob.scm reloading as you go.
  • Run (test) to check your solution.

Testing options

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

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.

test.scm

#!r6rs

(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)
  (call/cc
    (lambda (k)
      (with-exception-handler
        (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"
                description
                input
                result
                output)))
          `(pass . ,description))))))

(define (test-error description procedure input)
  (call/cc
    (lambda (k)
      (with-exception-handler
        (lambda (e) (k `(pass . ,description)))
        (lambda ()
          (test-run-solution procedure input)
          `(fail
             (description . ,description)
             (input . ,input)
             (output . error)
             (who . ,procedure)))))))

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

(define response-for)

(define (test . query)
  (apply
    run-test-suite
    (list
     (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.")
         "Whatever."))
     (lambda ()
       (test-success "forceful question" equal? response-for
         '("WHAT THE HELL WERE YOU THINKING?")
         "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")
         "Whatever."))
     (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.")
         "Whatever."))
     (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      ")
         "Whatever.")))
    query))

(let ([args (command-line)])
  (if (null? (cdr args)) (load "bob.scm") (load (cadr args)))
  (test 'input 'output))
(import (rnrs))

; (import srfi-1)
(import (srfi srfi-1))

(define (char-alphanumeric? c)
   (or (char-alphabetic? c) (char-numeric? c)))

(define (trim-start chars)
   (if (or (null? chars) (not (eq? (car chars) #\space)))
       chars
       (trim-start (cdr chars))))

(define (trim-end chars)
   (reverse (trim-start (reverse chars))))

(define (all-caps? chars)
   (not (find char-lower-case? chars)))

(define (ends-in-qmark? chars)
   (and (> (length chars) 0)
        (eq? #\? (last chars))))

(define (has-letters? chars)
   (find char-alphabetic? chars))

(define (silent? chars)
   (not (find char-alphanumeric? chars)))

(define (response-for message)
   (define cs (trim-end (string->list message)))
   (cond
      ((and (has-letters? cs)
            (all-caps? cs)
            (ends-in-qmark? cs)) "Calm down, I know what I'm doing!")
      ((and (has-letters? cs)
            (all-caps? cs)) "Whoa, chill out!")
      ((ends-in-qmark? cs) "Sure.")
      ((silent? cs) "Fine. Be that way!")
      (#t "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?