Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Learn You a Haskell shows the words function.

words and unwords are for splitting a line of text into words or joining a list of words into a text

Example:

ghci> words "hey these are the words in this sentence"
["hey","these","are","the","words","in","this","sentence"]
ghci> words "hey these           are    the words in this\nsentence"
["hey","these","are","the","words","in","this","sentence"]

Please critique my implementation.

words' :: String -> [String]
words' []  = []
words' xxs@(x:xs) 
  | x == ' '  = words' xs
  | otherwise = ys : words' rest
                  where (ys, rest) = break (== ' ') xxs
share|improve this question

1 Answer 1

up vote 4 down vote accepted

words treats any whitespace as a separator, not just spaces. Use Data.Char.isSpace.

It's fine otherwise.

When reimplementing the standard library, you can exploit the standard version as a reference implementation to compare your version to:

map (\x -> words x == words' x) ["", "  ", "a", "a ", " a", "a  b", "aa bb", "aa\nbb", "a b\nc\td"]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.