Word wrap: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied) |
|||
Line 2,421: | Line 2,421: | ||
Alternative greedy wrapping: <lang haskell>import Data.List (inits, |
Alternative greedy wrapping: <lang haskell>import Data.List (inits, tail, tails) |
||
⚫ | |||
testString = |
|||
wWrap n = |
|||
⚫ | |||
unlines |
|||
⚫ | |||
. map unwords |
|||
⚫ | |||
. wWrap'' n |
|||
⚫ | |||
. words |
|||
⚫ | |||
⚫ | |||
⚫ | |||
. lines |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
] |
|||
wWrap'' :: Int -> [String] -> [[String]] |
|||
wWrap'' _ [] = [] |
wWrap'' _ [] = [] |
||
wWrap'' |
wWrap'' n ss = |
||
(\(a, b) -> a : wWrap'' |
(\(a, b) -> a : wWrap'' n b) $ |
||
last . filter ((<= |
last . filter ((<= n) . length . unwords . fst) $ |
||
zip (inits ss) (tails ss) |
|||
⚫ | |||
wWrap i = unlines . map unwords . wWrap'' i . words . concat . lines |
|||
main :: IO () |
|||
main = putStrLn $ wWrap 80 testString</lang> |
|||
main = |
|||
putStrLn $ |
|||
wWrap 80 $ |
|||
concat |
|||
⚫ | |||
" there lived a king whose daughters were all", |
|||
⚫ | |||
⚫ | |||
" was astonished whenever, it shone in her", |
|||
⚫ | |||
⚫ | |||
" the forest was a well, and when the day was", |
|||
⚫ | |||
" forest and sat down by the side of the cool", |
|||
⚫ | |||
⚫ | |||
" caught it, and this ball was her favorite", |
|||
⚫ | |||
]</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |