DEV Community

Discussion on: Daily Challenge #135 - The Wide Mouthed Frog!

Collapse
 
avalander profile image
Avalander

Haskell

mouthSize :: String -> String
mouthSize "alligator" = "small"
mouthSize x           = "wide"
Collapse
 
aminnairi profile image
Amin • Edited

I see you are learning about Haskell. Congratulations! This isn't an easy language to handle (I'm am myself a learner, but aren't we all?).

I guess in your solution, cases like "ALLIGATOR" and "aLlIgAtOr" won't match the first pattern and will return "wide". But the OP was mentioning that the "alligator" animal was case insensitive.

So I'm proposing this solution.

import Data.Char (toLower, isSpace)
import Data.List (dropWhile, dropWhileEnd)

trim :: String -> String
trim = dropWhile isSpace . dropWhileEnd isSpace

mouthSize :: String -> String
mouthSize animal 
    | (trim $ map toLower animal) == "alligator" = "small"
    | otherwise = "wide"

main :: IO ()
main = do
    print $ mouthSize "alligator"       -- small
    print $ mouthSize "   alligator   " -- small
    print $ mouthSize "aLlIgAtOr"       -- small
    print $ mouthSize "fox"             -- wide

I took the opportunity to handle the cases where there was too much spaces (as I did in my TypeScript proposal).

This may not be the final solution, or prone to enhancement, but I guess it is a good one. We can make it better together as a community!