DEV Community

Discussion on: Daily Challenge #263 - Reverse Words

Collapse
 
aminnairi profile image
Amin • Edited

Haskell

import Data.List.Split (split, oneOf)


reverseWords :: String -> String
reverseWords = concatMap reverse . split (oneOf " ")


main :: IO ()
main = do
    print $ reverseWords "One space"        -- "enO ecaps"
    print $ reverseWords "Two  spaces"      -- "owT  secaps"
    print $ reverseWords "Three   spaces"   -- "eerhT   secaps"
Collapse
 
olekria profile image
Olek Ria

Very elegant.

Collapse
 
oleksandrriabukha profile image
Oleksandr Riabukha

Without libraries:
reverseWords = unwords . map reverse . words

Collapse
 
aminnairi profile image
Amin • Edited

This looks very clean, well done. I think I would have gone for that one if keeping spaces wasn't an issue, but it is in this challenge.

The second code example from this post is wrong in the sense that spaces must be kept, and only a few provided solutions here are working because the example was not clear enough on what was expected.

- "double spaces" ==> "elbuod secaps"
+ "double  spaces" ==> "elbuod  secaps"

I always lookup for the original challenge in Codewars to get a better specification to what I'm supposed to provide as a valid solution.

Original challenge specification.