Jump to content

Align columns: Difference between revisions

→‎{{header|Haskell}}: Or, using Text rather than String
(added Scheme example)
(→‎{{header|Haskell}}: Or, using Text rather than String)
Line 2,494:
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>
 
</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}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.