import System.IO asc r@('\n':_) (x:xs) = asc (x:r) xs asc r [] = drop 1 $ reverse r asc r@(s:sx) i@(x:xs) | x == ' ' = asc r xs | x == '\n' = asc ('\n':r) xs | s < x = asc (x:r) xs | otherwise = asc r xs main = interact (asc "\n")

Note that non-ascii characters in the above source code will be escaped (such as \x9f).