RosettaCodeData/Task/Word-wrap/Lambdatalk/word-wrap-3.lambdatalk
2023-07-01 13:44:08 -04:00

39 lines
2.6 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{def wrap2 // the function's name
{def wrap2.r // recursive "loop"
{lambda {:linewidth :a1 :spaceleft :a2}
{if {A.empty? :a1} // it's the end so ...
then {A.toS :a2} // ... return the sentence
else {let { {:linewidth :linewidth} // local variables
{:spaceleft :spaceleft} // lambdatalk has no closure
{:a1 :a1} // so we recall outers values
{:a2 :a2} // ... manually
{:word {A.first :a1}} // get the first word
{:length {W.length {A.first :a1}}} // compute its length
} {wrap2.r // do it again with
:linewidth // the linewidth unchanged
{A.rest :a1} // the rest of array
{if {> {+ :length 1} :spaceleft} // if ...
then {- :linewidth :length} // then update spaceleft and
{A.addlast! {br}:word :a2} // push a breakline and the word
else {- :spaceleft {+ :length 1}} // else update spaceleft and
{A.addlast! :word :a2}} }}}}} // push the word
{lambda {:linewidth :text} // a function with 2 arguments
{wrap2.r :linewidth // call the recursive "loop"
{A.rest {A.new :text}} // the input as an array minus first word
{- :linewidth {W.length {S.get 0 :text}}} // compute spaceleft
{A.new {S.get 0 :text}}}}} // the output as an array plus first word
-> wrap2
{wrap2 70 {text}} ->
Personne na sans doute oublié le terrible coup de vent de
nord-est qui se déchaîna au milieu de léquinoxe de cette
année, et pendant lequel le baromètre tomba à sept cent dix
millimètres. Ce fut un ouragan, sans intermittence, qui dura
du 18 au 26 mars. Les ravages quil produisit furent
immenses en Amérique, en Europe, en Asie, sur une zone large
de dix-huit cents milles, qui se dessinait obliquement à
léquateur, depuis le trente-cinquième parallèle nord
jusquau quarantième parallèle sud ! (Lîle mystérieuse /
Jules Verne)