Align columns: Difference between revisions
Content added Content deleted
(added Scheme example) |
(→{{header|Haskell}}: Or, using Text rather than String) |
||
Line 2,494: | Line 2,494: | ||
column are separated by at least one space. |
column are separated by at least one space. |
||
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. |
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}} |
|||
⚫ | |||
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}}== |