Skip to content

Instantly share code, notes, and snippets.

@yoshitsugu
Created February 10, 2016 00:17
Show Gist options
  • Save yoshitsugu/b36fe9130958fac26652 to your computer and use it in GitHub Desktop.
Save yoshitsugu/b36fe9130958fac26652 to your computer and use it in GitHub Desktop.
chunk :: (Eq b) => [a] -> (a -> b) -> [(b, [a])]
chunk [] _ = []
chunk (x:xs) f = let fx = f x
(h, t) = span ((==) fx . f) xs
in [(fx,(x:h))] ++ chunk t f
data ChunkType = Word | Blank deriving (Show, Eq)
chunkByBlank :: Char -> ChunkType
chunkByBlank ' ' = Blank
chunkByBlank _ = Word
main = putStrLn . show $ chunk "Haskell is awesome!!" chunkByBlank
-- => [(Word,"Haskell"),(Blank," "),(Word,"is"),(Blank," "),(Word,"awesome!!")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment