Reverse words in a string: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
No edit summary |
||
Line 1,777: | Line 1,777: | ||
----------------------- Robert Frost |
----------------------- Robert Frost |
||
</pre> |
</pre> |
||
=={{header|Lambdatalk}}== |
|||
This answer illustrates how a missing primitive (line_split) can be added directly in the wiki page. |
|||
<lang scheme> |
|||
1) We write a function |
|||
{def line_reverse |
|||
{def line_reverse.r |
|||
{lambda {:i :txt :length} |
|||
{if {> :i :length} |
|||
then |
|||
else {br}{A2S {A.reverse! {A.get :i :txt}}} |
|||
{line_reverse.r {+ :i 1} :txt :length}}}} |
|||
{lambda {:txt} |
|||
{let { {:a {line_split {:txt}}} } |
|||
{line_reverse.r 0 :a {- {A.length :a} 1}}}} } |
|||
-> line_reverse |
|||
where A2S translates an array into a sentence |
|||
{def A2S |
|||
{lambda {:a} |
|||
{if {A.empty? :a} |
|||
then |
|||
else {A.first :a} {A2S {A.rest :a}}}}} |
|||
-> A2S |
|||
and line_split is a javascript primitive directly written in the wiki page, |
|||
added to the dictionary and returning an array of lines |
|||
LAMBDATALK.DICT['line_split'] = function () { |
|||
var args = arguments[0].split("\n"); |
|||
var str = "{A.new "; |
|||
for (var i=0; i< args.length; i++) |
|||
str += "{A.new " + args[i] + "} "; |
|||
str += "}"; |
|||
return LAMBDATALK.eval_forms( str ) |
|||
}; |
|||
2) input (from a simple text source without any presetting) |
|||
{def rosetta |
|||
---------- Ice and Fire ------------ |
|||
fire, in end will world the say Some |
|||
ice. in say Some |
|||
desire of tasted I''ve what From |
|||
fire. favor who those with hold I |
|||
... elided paragraph last ... |
|||
Frost Robert ----------------------- |
|||
} |
|||
-> rosetta |
|||
3) calling the function: |
|||
{line_reverse rosetta} |
|||
-> |
|||
3) output |
|||
------------ Fire and Ice ---------- |
|||
Some say the world will end in fire, |
|||
Some say in ice. |
|||
From what I''ve tasted of desire |
|||
I hold with those who favor fire. |
|||
... last paragraph elided ... |
|||
----------------------- Robert Frost |
|||
</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |