1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
   10 
   11 
   12 
   13 
   14 
   15 
   16 
   17 
   18 
   19 
   20 
   21 
   22 
   23 
   24 
   25 
   26 
   27 
   28 
   29 
   30 
   31 
   32 
   33 
   34 
   35 
   36 
   37 
   38 
   39 
   40 
   41 
   42 
   43 
   44 
   45 
   46 
   47 
   48 
   49 
   50 
   51 
   52 
   53 
   54 
   55 
   56 
   57 
   58 
   59 
   60 
   61 
   62 
   63 
   64 
   65 
   66 
   67 
   68 
   69 
   70 
   71 
   72 
   73 
   74 
   75 
   76 
   77 
   78 
   79 
   80 
   81 
   82 
   83 
   84 
   85 
   86 
   87 
   88 
   89 
   90 
   91 
   92 
   93 
   94 
   95 
   96 
   97 
   98 
   99 
  100 
  101 
  102 
  103 
  104 
  105 
  106 
  107 
  108 
  109 
  110 
  111 
  112 
  113 
  114 
  115 
  116 
  117 
  118 
  119 
  120 
  121 
  122 
  123 
  124 
  125 
  126 
  127 
  128 
  129 
  130 
  131 
  132 
  133 
  134 
  135 
  136 
  137 
  138 
  139 
  140 
  141 
  142 
  143 
  144 
  145 
  146 
  147 
  148 
  149 
  150 
  151 
  152 
  153 
  154 
  155 
  156 
  157 
  158 
  159 
  160 
  161 
  162 
  163 
  164 
  165 
  166 
  167 
  168 
  169 
  170 
  171 
  172 
  173 
  174  module Main where
  175 
  176  import Char
  177  import IO
  178  import System
  179 
  180 
  181 
  182 
  183 
  184 
  185 
  186 
  187 
  188 
  189 
  190 
  191 
  192 
  193 
  194 
  195 
  196 
  197 
  198 
  199 
  200 
  201 
  202 
  203 
  204 
  205 
  206 
  207 
  208 
  209 
  210 
  211 
  212 
  213 
  214 
  215  fold1 :: (a->b->b) -> (a->b) -> [a] -> b
  216  fold1 f g [a]   = g a
  217  fold1 f g (a:x) = f a (fold1 f g x)
  218 
  219 
  220 
  221 
  222 
  223 
  224 
  225 
  226 
  227 
  228 
  229 
  230 
  231 
  232 
  233 
  234  scan1 :: (a->b->b) -> (a->b) -> [a] -> [b]
  235  scan1 f g = fold1 f' g'
  236              where g' a   = [g a]
  237                    f' a s = f a (head s) : s
  238 
  239 
  240 
  241 
  242 
  243 
  244 
  245 
  246  tails :: [a] -> [[a]]
  247  tails = scan1 (:) (:[])
  248 
  249 
  250 
  251 
  252 
  253 
  254 
  255 
  256 
  257 
  258 
  259 
  260 
  261 
  262 
  263 
  264 
  265 
  266 
  267 
  268 
  269 
  270  single :: [a] -> Bool
  271  single [a] = True
  272  single _   = False
  273 
  274 
  275 
  276 
  277 
  278 
  279 
  280 
  281 
  282 
  283 
  284 
  285 
  286  minWith :: (a->Int) -> [a] -> a
  287  minWith f = fold1 choice id
  288              where choice a b | f a <  f b = a
  289                               | otherwise  = b
  290 
  291 
  292 
  293 
  294 
  295 
  296 
  297 
  298 
  299 
  300 
  301 
  302 
  303 
  304 
  305 
  306  type Txt = [Word] 
  307  type Word = String
  308 
  309 
  310 
  311 
  312 
  313  type Paragraph = [Line]
  314  type Line = [Word] 
  315 
  316 
  317 
  318 
  319 
  320  par0 :: Txt -> Paragraph
  321  par0 = minWith cost . filter feasible . formats
  322 
  323 
  324 
  325 
  326 
  327 
  328 
  329 
  330 
  331 
  332 
  333 
  334 
  335 
  336 
  337 
  338 
  339  formats :: Txt -> [Paragraph]
  340  formats = fold1 next_word last_word
  341            where last_word w = [ [[w]] ]
  342                  next_word w ps = map (new w) ps ++ map (glue w) ps 
  343 
  344  new w ls      = [w]:ls
  345  glue w (l:ls) = (w:l):ls
  346 
  347 
  348 
  349 
  350 
  351 
  352 
  353 
  354 
  355 
  356 
  357 
  358 
  359 
  360  feasible :: Paragraph -> Bool
  361  feasible = all fits
  362 
  363 
  364 
  365 
  366 
  367 
  368 
  369 
  370  maxw :: Int
  371  maxw = 70
  372 
  373 
  374 
  375 
  376 
  377 
  378 
  379 
  380 
  381 
  382  fits :: Line -> Bool
  383  fits xs = (width xs <= maxw)
  384 
  385 
  386 
  387 
  388 
  389 
  390 
  391 
  392 
  393 
  394 
  395 
  396 
  397  width :: Line -> Int
  398  width = fold1 plus length
  399          where plus w n = length w + 1 + n
  400 
  401 
  402 
  403 
  404 
  405 
  406 
  407 
  408 
  409 
  410 
  411 
  412 
  413 
  414 
  415 
  416 
  417 
  418  optw :: Int
  419  optw = 63
  420 
  421 
  422 
  423 
  424 
  425 
  426 
  427 
  428 
  429 
  430 
  431 
  432  cost :: Paragraph -> Int
  433  cost = fold1 plus (const 0)
  434         where plus l n = linc l + n
  435               linc l = (optw - width l)^2
  436 
  437 
  438 
  439 
  440 
  441 
  442 
  443 
  444 
  445 
  446 
  447 
  448 
  449 
  450 
  451 
  452 
  453 
  454 
  455 
  456 
  457 
  458 
  459 
  460 
  461 
  462 
  463 
  464 
  465 
  466 
  467 
  468 
  469 
  470 
  471 
  472 
  473 
  474 
  475 
  476 
  477 
  478 
  479 
  480 
  481 
  482 
  483 
  484 
  485 
  486 
  487  par1 
  488   = minWith cost . fold1 step start
  489     where 
  490       step w ps = filter fitH (new w (minWith cost ps):map (glue w) ps)
  491       start w   = filter fitH [ [[w]] ]
  492  fitH = fits . head
  493 
  494 
  495 
  496 
  497 
  498 
  499 
  500 
  501 
  502 
  503 
  504 
  505 
  506 
  507 
  508 
  509 
  510 
  511 
  512 
  513 
  514 
  515  par1'
  516   = the . minWith cost . fold1 step start
  517     where 
  518       step w ps = filter fitH (new w (minWith cost ps):map (glue w) ps)
  519       start w   = filter fitH [([[w]], length w,0)]
  520       new w ([l],n,0)   = ([w]:[l], length w, 0)
  521       new w p           = ([w]:ls , length w, cost p) where (ls,n,m) = p
  522       glue w (l:ls,n,m) = ((w:l):ls, length w + 1 + n, m)
  523       the (ls,n,m)      = ls
  524       width_hd (ls,n,m) = n
  525       cost_tl (ls,n,m)  = m
  526       linc_hd p         = (optw - width_hd p)^2
  527       cost ([_],_,_)    = 0
  528       cost p            = linc_hd p + cost_tl p
  529       fitH p            = width_hd p <= maxw
  530 
  531 
  532 
  533 
  534 
  535 
  536 
  537 
  538 
  539 
  540 
  541 
  542 
  543 
  544 
  545 
  546 
  547 
  548 
  549 
  550 
  551 
  552 
  553 
  554 
  555 
  556 
  557 
  558 
  559 
  560 
  561 
  562 
  563 
  564 
  565 
  566 
  567 
  568 
  569 
  570 
  571 
  572 
  573 
  574 
  575 
  576 
  577 
  578 
  579 
  580 
  581 
  582 
  583 
  584 
  585 
  586 
  587 
  588 
  589 
  590 
  591 
  592 
  593 
  594 
  595 
  596 
  597 
  598 
  599 
  600 
  601 
  602 
  603 
  604 
  605 
  606 
  607 
  608 
  609 
  610 
  611 
  612 
  613 
  614 
  615 
  616 
  617 
  618 
  619 
  620 
  621 
  622 
  623 
  624 
  625 
  626 
  627 
  628 
  629 
  630 
  631 
  632 
  633 
  634 
  635 
  636 
  637 
  638 
  639 
  640 
  641 
  642 
  643 
  644 
  645 
  646 
  647 
  648 
  649 
  650 
  651 
  652 
  653 
  654 
  655 
  656 
  657 
  658 
  659 
  660  par2'
  661   = minWith cost . fold1 step start
  662     where 
  663       step w ps = trim (filter fitH (new w (minWith cost ps):map (glue w) ps))
  664       start w   = filter fitH [ [[w]] ]
  665       trim []   = []
  666       trim [p]  = [p]
  667       trim pspq 
  668         | cost p <= cost q = trim psp
  669         | otherwise        = trim psp ++ [q]
  670         where q   = last pspq
  671               psp = init pspq
  672               p   = last psp
  673               ps  = init psp
  674 
  675 
  676 
  677 
  678 
  679 
  680 
  681 
  682 
  683 
  684 
  685 
  686 
  687 
  688 
  689 
  690 
  691 
  692 
  693 
  694 
  695 
  696 
  697 
  698 
  699 
  700 
  701 
  702 
  703 
  704 
  705 
  706 
  707 
  708 
  709 
  710 
  711 
  712 
  713 
  714 
  715 
  716 
  717 
  718 
  719 
  720 
  721 
  722 
  723 
  724 
  725 
  726 
  727 
  728 
  729 
  730 
  731 
  732 
  733 
  734 
  735 
  736 
  737 
  738 
  739 
  740 
  741 
  742 
  743 
  744 
  745 
  746 
  747 
  748 
  749 
  750 
  751 
  752 
  753 
  754 
  755 
  756 
  757 
  758 
  759 
  760 
  761 
  762 
  763 
  764 
  765 
  766 
  767 
  768 
  769 
  770 
  771 
  772 
  773 
  774 
  775 
  776 
  777 
  778 
  779 
  780 
  781 
  782 
  783 
  784 
  785 
  786 
  787 
  788 
  789 
  790 
  791 
  792 
  793 
  794 
  795 
  796 
  797 
  798 
  799 
  800 
  801 
  802 
  803 
  804 
  805 
  806 
  807 
  808 
  809 
  810 
  811 
  812 
  813 
  814 
  815 
  816 
  817 
  818 
  819 
  820 
  821 
  822 
  823 
  824 
  825 
  826 
  827 
  828 
  829 
  830 
  831 
  832 
  833 
  834 
  835 
  836 
  837 
  838 
  839 
  840 
  841 
  842 
  843 
  844 
  845 
  846 
  847 
  848 
  849 
  850 
  851 
  852 
  853 
  854 
  855 
  856 
  857 
  858 
  859 
  860 
  861 
  862 
  863 
  864 
  865 
  866 
  867 
  868 
  869 
  870 
  871 
  872 
  873 
  874 
  875 
  876 
  877 
  878 
  879 
  880 
  881 
  882 
  883 
  884 
  885 
  886 
  887 
  888 
  889 
  890 
  891 
  892 
  893 
  894 
  895 
  896 
  897 
  898 
  899 
  900 
  901 
  902 
  903 
  904 
  905 
  906 
  907 
  908 
  909 
  910 
  911 
  912 
  913 
  914 
  915 
  916 
  917 
  918 
  919 
  920 
  921 
  922 
  923  par2
  924   = last . fold1 step start
  925     where 
  926       step w ps = trim(filter fitH (new w (last ps) `add` map (glue w) ps))
  927       start w   = filter fitH [ [[w]] ]
  928       add p []                          = [p]
  929       add p [q]                         = [p,q]
  930       add p (q:r:rs) | bf p q <= bf q r = add p (r:rs)
  931                      | otherwise        = p:q:r:rs
  932       bf p q
  933         | single q && cost pt == 0 
  934                     = (optw - wph) `min` rqh
  935         | single q  = rqh
  936         | otherwise = ceildiv (cost p - cost q) (2*(wqh-wph)) `min` rqh
  937           where ph:pt = p
  938                 qh:qt = q
  939                 wph   = width ph
  940                 wqh   = width qh
  941                 rqh   = maxw - wqh + 1
  942                 ceildiv n m = (n+m-1) `div` m
  943       trim []                      = []
  944       trim [p]                     = [p]
  945       trim pspq | cost p <= cost q = trim psp
  946                 | otherwise        = pspq
  947         where q   = last pspq
  948               psp = init pspq
  949               p   = last psp
  950               ps  = init psp
  951 
  952 
  953 
  954 
  955 
  956 
  957 
  958 
  959 
  960 
  961 
  962 
  963 
  964 
  965 
  966 
  967 
  968 
  969 
  970 
  971 
  972 
  973 
  974 
  975 
  976 
  977 
  978 
  979 
  980 
  981 
  982 
  983 
  984 
  985 
  986 
  987 
  988 
  989 
  990 
  991 
  992 
  993 
  994 
  995 
  996 
  997 
  998 
  999 
 1000 
 1001 
 1002 
 1003 
 1004 
 1005 
 1006 
 1007 
 1008 
 1009 
 1010 
 1011 
 1012 
 1013 
 1014 
 1015 
 1016 
 1017 
 1018 
 1019 
 1020 
 1021 
 1022 
 1023 
 1024 
 1025 
 1026 
 1027 
 1028 
 1029 
 1030 
 1031 
 1032 
 1033 
 1034 
 1035 
 1036 
 1037 
 1038 
 1039 
 1040 
 1041 
 1042 
 1043 
 1044 
 1045 
 1046 
 1047 
 1048 
 1049 
 1050 
 1051  par2''
 1052   = last . fold1 step start
 1053     where
 1054       step w ps = trim(droptail (not.fitH) (new w (last ps) `add` map (glue w) ps))
 1055       start w   = droptail (not.fitH) [ [[w]] ]
 1056       droptail p []              = []
 1057       droptail p xsx | p x       = droptail p xs
 1058                      | otherwise = xsx
 1059         where x  = last xsx
 1060               xs = init xsx
 1061       add p []                          = [p]
 1062       add p [q]                         = [p,q]
 1063       add p (q:r:rs) | bf p q <= bf q r = add p (r:rs)
 1064                      | otherwise        = p:q:r:rs
 1065       bf p q
 1066         | single q && cost pt == 0 
 1067                     = (optw - wph) `min` rqh
 1068         | single q  = rqh
 1069         | otherwise = ceildiv (cost p - cost q) (2*(wqh-wph)) `min` rqh
 1070           where ph:pt = p
 1071                 qh:qt = q
 1072                 wph   = width ph
 1073                 wqh   = width qh
 1074                 rqh   = maxw - wqh + 1
 1075                 ceildiv n m = (n+m-1) `div` m
 1076       trim []                      = []
 1077       trim [p]                     = [p]
 1078       trim pspq | cost p <= cost q = trim psp
 1079                 | otherwise        = pspq
 1080         where q   = last pspq
 1081               psp = init pspq
 1082               p   = last psp
 1083               ps  = init psp
 1084 
 1085 
 1086 
 1087 
 1088 
 1089 
 1090 
 1091 
 1092 
 1093 
 1094 
 1095 
 1096 
 1097 
 1098 
 1099 
 1100 
 1101 
 1102 
 1103 
 1104 
 1105 
 1106 
 1107 
 1108 
 1109 
 1110 
 1111 
 1112 
 1113 
 1114 
 1115 
 1116 
 1117 
 1118 
 1119 
 1120 
 1121 
 1122 
 1123 
 1124 
 1125 
 1126 
 1127 
 1128  type Par    = (Width,Cost,Length) 
 1129  type Width  = Int
 1130  type Cost   = Int
 1131  type Length = Int
 1132 
 1133  width_tl = fst3
 1134  cost_tl  = snd3
 1135  len_tl   = thd3
 1136 
 1137 
 1138 
 1139 
 1140 
 1141 
 1142  fst3 (a,b,c) = a
 1143  snd3 (a,b,c) = b
 1144  thd3 (a,b,c) = c
 1145 
 1146 
 1147 
 1148 
 1149 
 1150 
 1151 
 1152 
 1153 
 1154 
 1155 
 1156 
 1157 
 1158 
 1159 
 1160 
 1161 
 1162 
 1163 
 1164 
 1165 
 1166 
 1167  tile :: Txt -> ([Length],Length) -> Paragraph
 1168  tile ws ([],n)   = []
 1169  tile ws (m:ms,n) = ws1 : tile ws2 (drop l (m:ms),m)
 1170                     where l = n - m
 1171                           (ws1,ws2) = splitAt l ws
 1172 
 1173 
 1174 
 1175 
 1176 
 1177 
 1178 
 1179 
 1180 
 1181 
 1182 
 1183 
 1184 
 1185 
 1186 
 1187 
 1188 
 1189 
 1190 
 1191 
 1192 
 1193 
 1194 
 1195 
 1196 
 1197 
 1198 
 1199 
 1200 
 1201 
 1202 
 1203 
 1204 
 1205 
 1206 
 1207 
 1208 
 1209 
 1210 
 1211 
 1212 
 1213 
 1214 
 1215 
 1216 
 1217 
 1218 
 1219 
 1220 
 1221 
 1222 
 1223 
 1224 
 1225 
 1226 
 1227 
 1228 
 1229  par3 :: Txt -> Paragraph 
 1230  par3 ws
 1231   = tile ws (map (len_tl.last'.fst3) zs, thd3 (head zs))
 1232     where zs = scan1 stepr startr (map length ws)
 1233 
 1234 
 1235 
 1236 
 1237 
 1238 
 1239 
 1240 
 1241 
 1242 
 1243 
 1244 
 1245  startr :: Length -> (SymList Par, Width, Length)
 1246  startr a | a <= maxw = (cons' (0,0,0) nil',a,1)
 1247 
 1248 
 1249 
 1250 
 1251 
 1252 
 1253 
 1254 
 1255 
 1256 
 1257 
 1258 
 1259  stepr :: Length -> 
 1260          (SymList Par, Cost, Length) -> 
 1261          (SymList Par, Cost, Length)
 1262  stepr w (ps,tw,tl)  
 1263   = (trim (drop_nofit (new (last' ps) `add` ps)), tot_width, tot_len)
 1264     where 
 1265       single p      = len_tl p == 0
 1266       cost p 
 1267         | single p  = 0
 1268         | otherwise = cost_tl p + (optw - width_hd p)^2
 1269       width_hd p
 1270         | single p  = tot_width
 1271         | otherwise = tot_width - width_tl p - 1
 1272       tot_width     = w + 1 + tw
 1273       tot_len       = 1 + tl
 1274 
 1275 
 1276 
 1277 
 1278 
 1279 
 1280 
 1281 
 1282       new p | single p  = (tw,0,tl)
 1283             | otherwise = (tw,cost_tl p + (optw-old_width_hd p)^2,tl)
 1284       old_width_hd p | single p  = tw
 1285                      | otherwise = tw - width_tl p - 1
 1286 
 1287 
 1288 
 1289 
 1290 
 1291       trim ps_pq | null' ps_pq      = ps_pq
 1292                  | single' ps_pq    = ps_pq
 1293                  | cost p <= cost q = trim ps_p
 1294                  | otherwise        = ps_pq
 1295                    where ps_p = init' ps_pq
 1296                          q    = last' ps_pq
 1297                          p    = last' ps_p
 1298 
 1299 
 1300 
 1301 
 1302 
 1303 
 1304 
 1305       drop_nofit ps_p | null' ps_p        = ps_p
 1306                       | width_hd p > maxw = drop_nofit ps
 1307                       | otherwise         = ps_p
 1308                         where ps = init' ps_p
 1309                               p  = last' ps_p
 1310 
 1311 
 1312 
 1313 
 1314 
 1315 
 1316 
 1317 
 1318 
 1319 
 1320 
 1321 
 1322       add p qr_rs | single' qr_rs || null' qr_rs = cons' p qr_rs
 1323                   | bf p q <= bf q r             = add p r_rs
 1324                   | otherwise                    = cons' p qr_rs
 1325                     where r_rs = tail' qr_rs
 1326                           q  = head' qr_rs
 1327                           r  = head' r_rs
 1328 
 1329 
 1330 
 1331 
 1332 
 1333       bf p q 
 1334         | single q && cost_tl p == 0 = (optw - wph) `min` rqh 
 1335         | single q                   = rqh
 1336         | otherwise                  = ceildiv (cost p-cost q) 
 1337                                                (2*(wqh-wph)) `min` rqh
 1338            where
 1339              wph = width_hd p
 1340              wqh = width_hd q
 1341              rqh = maxw - wqh + 1
 1342 
 1343  ceildiv n m = (n+m-1) `div` m
 1344 
 1345 
 1346 
 1347 
 1348 
 1349 
 1350 
 1351 
 1352 
 1353 
 1354 
 1355 
 1356 
 1357 
 1358 
 1359 
 1360 
 1361 
 1362 
 1363 
 1364 
 1365 
 1366 
 1367 
 1368 
 1369 
 1370 
 1371 
 1372  fmt = unparse . map (par3 . concat) . parse
 1373 
 1374 
 1375 
 1376 
 1377 
 1378  fmtWith par = unparse . map (par . concat) . parse
 1379 
 1380 
 1381 
 1382 
 1383 
 1384 
 1385 
 1386 
 1387 
 1388 
 1389 
 1390 
 1391 
 1392 
 1393 
 1394 
 1395 
 1396 
 1397 
 1398 
 1399 
 1400 
 1401 
 1402 
 1403 
 1404 
 1405 
 1406 
 1407 
 1408 
 1409 
 1410 
 1411 
 1412 
 1413 
 1414 
 1415 
 1416 
 1417 
 1418 
 1419 
 1420 
 1421 
 1422 
 1423 
 1424 
 1425 
 1426 
 1427 
 1428 
 1429 
 1430 
 1431 
 1432 
 1433 
 1434 
 1435 
 1436 
 1437 
 1438 
 1439 
 1440 
 1441 
 1442 
 1443 
 1444 
 1445 
 1446 
 1447 
 1448 
 1449 
 1450 
 1451 
 1452 
 1453 
 1454 
 1455 
 1456 
 1457 
 1458 
 1459 
 1460 
 1461 
 1462 
 1463 
 1464 
 1465 
 1466 
 1467 
 1468 
 1469 
 1470 
 1471 
 1472 
 1473 
 1474 
 1475 
 1476 
 1477 
 1478 
 1479 
 1480 
 1481 
 1482 
 1483 
 1484 
 1485 
 1486 
 1487 
 1488 
 1489 
 1490 
 1491 
 1492 
 1493 
 1494 
 1495 
 1496 
 1497 
 1498 
 1499 
 1500 
 1501 
 1502 
 1503 
 1504 
 1505 
 1506 
 1507 
 1508 
 1509 
 1510 
 1511 
 1512 
 1513 
 1514 
 1515 
 1516 
 1517 
 1518 
 1519 
 1520 
 1521 
 1522 
 1523 
 1524 
 1525 
 1526 
 1527 
 1528 
 1529 
 1530 
 1531 
 1532 
 1533 
 1534 
 1535 
 1536 
 1537 
 1538 
 1539 
 1540 
 1541 
 1542 
 1543 
 1544 
 1545 
 1546 
 1547 
 1548 
 1549 
 1550 
 1551 
 1552 
 1553 
 1554 
 1555 
 1556 
 1557 
 1558 
 1559 
 1560 
 1561 
 1562 
 1563 
 1564 
 1565 
 1566 
 1567 
 1568 
 1569 
 1570 
 1571 
 1572 
 1573 
 1574 
 1575 
 1576 
 1577 
 1578 
 1579 
 1580 
 1581 
 1582 
 1583 
 1584 
 1585 
 1586 
 1587 
 1588 
 1589 
 1590 
 1591 
 1592 
 1593 
 1594 
 1595 
 1596 
 1597 
 1598 
 1599 
 1600 
 1601 
 1602 
 1603 
 1604 
 1605 
 1606 
 1607 
 1608 
 1609 
 1610 
 1611 
 1612 
 1613 
 1614 
 1615 
 1616 
 1617 
 1618 
 1619 
 1620 
 1621 
 1622 
 1623 
 1624 
 1625 
 1626 
 1627 
 1628 
 1629 
 1630 
 1631 
 1632 
 1633 
 1634 
 1635 
 1636 
 1637 
 1638 
 1639 
 1640 
 1641 
 1642 
 1643 
 1644 
 1645 
 1646 
 1647 
 1648 
 1649 
 1650 
 1651 
 1652 
 1653 
 1654 
 1655 
 1656 
 1657 
 1658 
 1659 
 1660 
 1661 
 1662 
 1663 
 1664  type SymList a = ([a],[a])
 1665 
 1666  single' (x,y) = (null x && single y) || (single x && null y)
 1667 
 1668  null' ([],[]) = True
 1669  null' _       = False
 1670 
 1671  nil' = ([],[])
 1672 
 1673  head' (x,y) | not (null x) = head x
 1674              | otherwise = head y
 1675 
 1676  last' (y,x) | not (null x) = head x
 1677              | otherwise = head y
 1678 
 1679  cons' a (x,y) | not (null y) = (a:x,y)
 1680                | otherwise = ([a],x)
 1681 
 1682  snoc' a (y,x) | not (null y) = (y,a:x)
 1683                | otherwise = (x,[a])
 1684 
 1685  tail' (x,y) | null x    = ([],[])
 1686              | single x  = (reverse y1, y0)
 1687              | otherwise = (tail x, y)
 1688                where (y0,y1) = splitAt (length y `div` 2) y
 1689 
 1690  init' (y,x) | null x    = ([],[])
 1691              | single x  = (y0, reverse y1)
 1692              | otherwise = (y, tail x)
 1693                where (y0,y1) = splitAt (length y `div` 2) y
 1694 
 1695 
 1696 
 1697 
 1698 
 1699 
 1700 
 1701 
 1702 
 1703 
 1704 
 1705 
 1706 
 1707 
 1708 
 1709 
 1710  unformat :: a -> [[a]] -> [a]
 1711  unformat a = fold1 insert id
 1712           where insert xs ys = xs ++ [a] ++ ys
 1713 
 1714  format :: Eq a => a -> [a] -> [[a]]
 1715  format a [] = [[]]
 1716  format a x  = fold1 (break a) (start a) x
 1717         where break a b xs | a == b    = []:xs
 1718                            | otherwise = (b:head xs):tail xs
 1719               start a b = break a b [[]]
 1720 
 1721 
 1722 
 1723 
 1724 
 1725 
 1726 
 1727  unparas :: [[[String]]] -> [[String]]
 1728  unparas = unformat []
 1729 
 1730  paras :: [[String]] -> [[[String]]]
 1731  paras   = filter (/=[]) . format []
 1732 
 1733  parse    = paras . map words . lines
 1734  unparse  = unlines . map unwords . unparas
 1735 
 1736 
 1737 
 1738 
 1739 
 1740 
 1741 
 1742  parg :: Txt -> Paragraph
 1743  parg = foldl nextword [[]]
 1744    where
 1745      nextword p w | fits (last p++[w]) = init p ++ [last p ++ [w]]
 1746                   | otherwise = p ++ [[w]]
 1747  fmtg = fmtWith parg
 1748 
 1749 
 1750 
 1751 
 1752 
 1753  fmt1 = fmtWith par1
 1754 
 1755 
 1756 
 1757 
 1758  test = 
 1759    "In the constructive programming community it is commonplace to see " ++
 1760    "formal developments of textbook algorithms. In the algorithm design " ++
 1761    "community, on the other hand, it may be well known that the textbook " ++
 1762    "solution to a problem is not the most efficient possible. However, in " ++
 1763    "presenting the more efficient solution, the algorithm designer will " ++
 1764    "usually omit some of the implementation details, this creating an " ++
 1765    "algorithm gap between the abstract algorithm and its concrete " ++
 1766    "implementation. This is in contrast to the formal development, which " ++
 1767    "usually presents the complete concrete implementation of the less " ++
 1768    "efficient solution. \n \n"
 1769 
 1770  tests = concat (repeat test)
 1771 
 1772  main = getArgs >>= (\as ->
 1773         if length as /= 1
 1774         then putStr "usage: para <file name>"
 1775         else openFile (head as) ReadMode >>= (\h ->
 1776              hGetContents h >>= (\ws ->
 1777              putStr (if null ws then [] else (fmt ws)))))
 1778 
 1779 
 1780 
 1781