Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created May 11, 2020 16:28
Show Gist options
  • Save lgastako/75ca3e4877af65e4111ebc4f9a19e2e8 to your computer and use it in GitHub Desktop.
Save lgastako/75ca3e4877af65e4111ebc4f9a19e2e8 to your computer and use it in GitHub Desktop.
words'' :: String -> [String]
words'' s
| s == "" = []
| otherwise = takeWhile (/= ' ') s : words'' (dropWhile (== ' ') (dropWhile (/= ' ') s))
words' :: String -> [String]
words' s
| w /= "" = w : words' rest
| otherwise = []
where
w = takeWhile (/= ' ') . dropWhile (== ' ') $ s
rest = dropWhile (/= ' ') . dropWhile (== ' ') $ s
demo :: (String -> [String]) -> [[String]]
demo f = map f
[ ""
, " "
, " "
, " abc"
, "def "
, " ghi"
, "jkl "
, "mno pqr"
, " stu vwx "
, " a b cd efg h i "
]
test :: (String -> [String]) -> Bool
test f = demo f == demo words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment