Avatar of agbell

agbell's solution

to Bob in the Haskell Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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.

Getting Started

For installation and learning resources, refer to the exercism help page.

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

You are probably running an old stack version and need to upgrade it.

Otherwise, if you get an error message like this...

No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...

Just do as it says and it will download and install the correct compiler version:

stack setup

Running GHCi

If you want to play with your solution in GHCi, just run the command:

stack ghci

Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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.

Tests.hs

{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Bob (responseFor)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "responseFor" $ for_ cases test
  where
    test Case{..} = it description $ responseFor input `shouldBe` expected

data Case = Case { description :: String
                 , input       :: String
                 , expected    :: String
                 }

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

import Data.Char(isSpace, isUpper, isAlpha)
import Data.List(isSuffixOf)

data Answer = Question | Yell | Blank | Otherwise

instance Show Answer where
  show Question = "Sure."
  show Yell = "Woah, chill out!"
  show Blank = "Fine. Be that way!"
  show Otherwise = "Whatever."

responseFor :: String -> String
responseFor input = show toAnswer
 where
   toAnswer
    | all isSpace input = Blank
    | all isUpper alphaInput && not (null alphaInput) = Yell
    | "?" `isSuffixOf` input = Question
    | otherwise = Otherwise
   alphaInput = filter isAlpha input

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of agbell

cleaning up import and exports as @DougBrunner

switching to using isSuffixOf improve readability of Yell option

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?