Crunch

Posted on 2015-07-26 by nbloomf
Tags: literate-haskell, munge

This post is literate Haskell; you can load the source into GHCi and play along.


Remove repeated spaces or tabs.

module Crunch where

crunch :: String -> String
crunch []            = []
crunch [' ']         = []
crunch ['\t']        = []
crunch [c]           = [c]
crunch (' ':' ':cs)  = crunch (' ':cs)
crunch (' ':'\t':cs) = crunch (' ':cs)
crunch (' ':'\n':cs) = '\n' : crunch cs
crunch ('\t':cs)     = crunch (' ':cs)
crunch (c:cs)        = c : crunch cs

main :: IO ()
main = interact crunch