Created
June 3, 2018 02:36
-
-
Save friedbrice/bfb8ab98689bb05cc67bf501f06a0bad to your computer and use it in GitHub Desktop.
Delimit and Rejoin List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Delimit where | |
import Data.Char (isLetter) | |
delimit :: (a -> Bool) -> [a] -> ([a], [[a]]) | |
delimit p = foldr step ([], [[]]) where | |
step c (ds, w:ws) = if p c then (c:ds, []:w:ws) else (ds, (c:w):ws) | |
rejoin :: [a] -> [[a]] -> [a] | |
rejoin ds ws = foldr (\(w, d) acc -> w ++ d:acc) [] (zip ws ds) ++ last ws | |
example :: String | |
example = "this is some mad, silly-ass shit" | |
test :: IO () | |
test = if uncurry rejoin (delimit (not.isLetter) example) == example | |
then putStrLn "Test Passed!" | |
else error "Test Failed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need this so frequently that I'll just put it here for safe keeping ^_^;