Sunday 5 June 2016

Haskell: Remove all spaces from a string


SpaceRemover.hs
removeSpaces :: String -> String
removeSpaces [] = []
removeSpaces (x:xs)
    | (x == ' ') = removeSpaces xs
    | otherwise = x : removeSpaces (xs)

Prelude> :load SpaceRemover.hs 
[1 of 1] Compiling Main             ( SpaceRemover.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> removeSpaces []
""
*Main> removeSpaces "Hello How are you"
"HelloHowareyou"
*Main> 
*Main> removeSpaces "Hello How are you   "
"HelloHowareyou"
*Main> removeSpaces "    Hello How are you   "
"HelloHowareyou"


Previous                                                 Next                                                 Home

No comments:

Post a Comment