Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/466469274467704833
formatting
Source Link
200_success
  • 145.7k
  • 22
  • 192
  • 481

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"]

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

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

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
Source Link
Kevin Meredith
  • 3.2k
  • 29
  • 52

Implementing Haskell#words

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