Skip to content

Instantly share code, notes, and snippets.

@polux
Created September 24, 2012 23:31
Show Gist options
  • Save polux/3779102 to your computer and use it in GitHub Desktop.
Save polux/3779102 to your computer and use it in GitHub Desktop.
JJ's problem
import Data.Char
main = interact unindent
unindent = unlines . unindentLines . lines
unindentLines ls = map (drop commonPrefixLength) ls
where -- the number of characters to drop from each line
commonPrefixLength = length (commonPrefix prefixes)
-- whitespace-only prefixes of non-empty lines of ls
prefixes = map (takeWhile isSpace) nonEmptyLines
-- the non-empty lines of ls
nonEmptyLines = filter (not . all isSpace) ls
-- the common prefix of a list of lists
commonPrefix [] = []
commonPrefix xss = foldr1 commonPrefix' xss
where -- the common prefix of two lists
commonPrefix' xs ys = map fst (takeWhile (uncurry (==)) (zip xs ys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment