630067ad4c10410798d1550152f20d41

Rotates text given in stdin 90 degrees.

1
2
3
4
5
6
7
8
9
10
11
12
main = do s <- getContents
          putStr (unlines (reverse (rotate (lines s))))

rotate ls = if (maxLength ls) > 0 then (column ls) : (rotate (dropColumn ls))
                                  else []

maxLength ls = (maximum (map length ls))
column ls = map wholeHead ls
dropColumn ls = map (drop 1) ls

wholeHead cs = if (null cs) then ' '
                            else (head cs)

Refactorings

No refactoring yet !

Avatar

newacct

November 12, 2008, November 12, 2008 00:33, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
main = do s <- getContents
          putStr $ unlines $ reverse $ rotate $ lines s

rotate ls | all null ls = []
          | otherwise   = column ls : rotate (dropColumn ls)

column ls = map wholeHead ls
dropColumn ls = map (drop 1) ls

wholeHead [] = ' '
wholeHead cs = head cs
4f1a44133b6d68f9cd191c39aa7c986b

Anders

March 13, 2009, March 13, 2009 02:01, permalink

No rating. Login to rate!

Data.List has a function 'transpose' that does what you want, minus the space fill.

1
2
3
4
5
6
7
import Data.List (transpose)

main = putStr . unlines . reverse . rotate . lines =<< getContents

rotate xss = transpose $ spaceFill (maxLen xss) xss
spaceFill n = map $ take n . (++ repeat ' ')
maxLength = maximum . map length
4f1a44133b6d68f9cd191c39aa7c986b

Anders

March 13, 2009, March 13, 2009 02:07, permalink

No rating. Login to rate!

I meant maxLength on line 5, of course. Sorry.

Your refactoring





Format Copy from initial code

or Cancel