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.
For installation and learning resources, refer to the exercism help page.
To run the test suite, execute the following command:
stack test
No .cabal file found in directory
You are probably running an old stack version and need to upgrade it.
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
If you want to play with your solution in GHCi, just run the command:
stack ghci
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!
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
{-# 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.String.Utils
import Data.Char
responseFor :: String -> String
responseFor statement
| isSilence statement = "Fine. Be that way!"
| isShouting statement = "Whoa, chill out!"
| isQuestion statement = "Sure."
| otherwise = "Whatever."
isSilence :: String -> Bool
isSilence statement = strip statement == ""
isShouting :: String -> Bool
isShouting statement =
map toUpper statement == statement &&
map toLower statement /= statement
isQuestion :: String -> Bool
isQuestion statement = endswith "?" (strip statement)
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments