Created
September 24, 2012 23:31
-
-
Save polux/3779102 to your computer and use it in GitHub Desktop.
JJ's problem
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
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