Skip to content

Instantly share code, notes, and snippets.

@yashsavani
Created February 1, 2020 23:25
Show Gist options
  • Save yashsavani/0750aec22700c254c977f6dd9f85dfaa to your computer and use it in GitHub Desktop.
Save yashsavani/0750aec22700c254c977f6dd9f85dfaa to your computer and use it in GitHub Desktop.
Reimplemented words function from haskell Data.Char
import Data.Char (isSpace)
words' :: String -> [String]
words' [] = []
words' ls = tail (foldr step [] (' ':ls))
where
step x (y:ys)
| isSpace x = if null y then []:ys else []:y:ys
| otherwise = (x:y):ys
step x _
| isSpace x = []
| otherwise = [[x]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment