Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Created June 3, 2018 02:36
Show Gist options
  • Save friedbrice/bfb8ab98689bb05cc67bf501f06a0bad to your computer and use it in GitHub Desktop.
Save friedbrice/bfb8ab98689bb05cc67bf501f06a0bad to your computer and use it in GitHub Desktop.
Delimit and Rejoin List
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!"
@friedbrice
Copy link
Author

I need this so frequently that I'll just put it here for safe keeping ^_^;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment