Align columns: Difference between revisions

added Haskell
(For single word columns <a href="http://en.wikipedia.org/wiki/Justification_(typesetting)"> justified</a> seems to be OK and fits the example.)
(added Haskell)
Line 178:
OD
END</pre>
 
=={{header|Haskell}}==
<pre>
import Data.List
import Control.Monad
import Control.Arrow
 
dat = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" ++
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" ++
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n" ++
"column$are$separated$by$at$least$one$space.\n" ++
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n" ++
"justified,$right$justified,$or$center$justified$within$its$column.\n"
 
brkdwn = unfoldr (\xs -> if null xs then Nothing else Just (second (drop 1) (span ('$'/=) xs)) )
spas = repeat ' '
 
format j ls = map (concat. zipWith align colw) rows
where
rows = map brkdwn $ lines ls
colw = map (maximum. map length) . transpose $ rows
align cw w =
case j of
'c' -> (take l spas) ++ w ++ (take (succ r) spas)
'r' -> (take dl spas)++w++" "
'l' -> w ++ (take (succ dl) spas)
where
dl = cw-length w
hdl = dl `div` 2
(l,r)
| odd dl = (hdl,hdl+1)
| otherwise = (hdl,hdl)
</pre>
output example:
<pre>
*Main> mapM_ putStrLn $ format 'c' dat
Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program
that aligns each column of fields by ensuring that words in each
column are separated by at least one space.
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
</pre>
 
=={{header|J}}==
Anonymous user