Align columns: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Or, using Text rather than String)
(→‎{{header|Haskell}}: Removed second version for repair)
Line 2,495: Line 2,495:
Further, allow for each word in a column to be either left
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.</pre>
justified, right justified, or center justified within its column.</pre>


Or, using the Text module rather than Strings:
<lang haskell>import Data.List as L
import Data.Text as T
import Data.Ord (comparing)

cols :: [[Text]]
cols =
L.transpose $
(splitOn (pack "$") . pack) <$>
[ "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."
]

main :: IO ()
main =
mapM_ putStrLn $
[ (\cols f ->
(unpack . T.unlines) $
T.unwords <$> L.transpose ((\(xs, n) -> f (n + 1) ' ' <$> xs) <$> cols))
(L.zip cols ((T.length . maximumBy (comparing T.length)) <$> cols))
] <*>
[justifyLeft, justifyRight, center]</lang>
{{Out}}
<pre>
Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program in each
that aligns each column of fields by ensuring that words either left
column are separated by at least one space. to be
Further, allow for each word in a column column.
justified, right justified, or center justified within its

Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program in each
that aligns each column of fields by ensuring that words either left
column are separated by at least one space. to be
Further, allow for each word in a column column.
justified, right justified, or center justified within its

Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program in each
that aligns each column of fields by ensuring that words either left
column are separated by at least one space. to be
Further, allow for each word in a column column.
justified, right justified, or center justified within its </pre>


=={{header|HicEst}}==
=={{header|HicEst}}==