Odd word problem: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|REXX}}: fixed a stray blank that was in a comment.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 101:
we,are;not,in,kansas;any,more.
we,era;not,ni,kansas;yna,more.</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
Line 1,735 ⟶ 1,736:
we,era;not,ni,kansas;yna,more.
</pre>
 
 
=={{header|Perl}}==
Line 1,821:
}
}</lang>
 
=={{header|Perl 6}}==
A recursive solution, with the added feature that it treats each line separately.
<lang perl6>my &in = { $*IN.getc // last }
 
loop {
ew(in);
ow(in).print;
 
multi ew ($_ where /\w/) { .print; ew(in); }
multi ew ($_) { .print; next when "\n"; }
 
multi ow ($_ where /\w/) { ow(in) x .print; }
multi ow ($_) { $_; }</lang>
{{out}}
<pre>$ ./oddword
we,are;not,in,kansas;any,more.
we,era;not,ni,kansas;yna,more.
what,is,the;meaning,of:life.
what,si,the;gninaem,of:efil.</pre>
Note how the even/oddness is reset on the line boundary; if not, the second line might have started out in an odd state and reversed "what" instead of "is". The call to <tt>next</tt> prevents that by sending the loop back to its initial state.
 
There is one clever trick here with the <tt>x</tt> operator; it evaluates both its arguments in order, but in this case only returns the left argument because the right one is always 1 (True). You can think of it as a reversed C-style comma operator.
 
=={{header|Phix}}==
Line 2,025 ⟶ 2,001:
true .
</pre>
 
=={{header|PureBasic}}==
This example uses recursion.
Line 2,229 ⟶ 2,206:
;; ;; -> we,era;not,ni,kansas;yna,more.
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
A recursive solution, with the added feature that it treats each line separately.
<lang perl6>my &in = { $*IN.getc // last }
 
loop {
ew(in);
ow(in).print;
 
multi ew ($_ where /\w/) { .print; ew(in); }
multi ew ($_) { .print; next when "\n"; }
 
multi ow ($_ where /\w/) { ow(in) x .print; }
multi ow ($_) { $_; }</lang>
{{out}}
<pre>$ ./oddword
we,are;not,in,kansas;any,more.
we,era;not,ni,kansas;yna,more.
what,is,the;meaning,of:life.
what,si,the;gninaem,of:efil.</pre>
Note how the even/oddness is reset on the line boundary; if not, the second line might have started out in an odd state and reversed "what" instead of "is". The call to <tt>next</tt> prevents that by sending the loop back to its initial state.
 
There is one clever trick here with the <tt>x</tt> operator; it evaluates both its arguments in order, but in this case only returns the left argument because the right one is always 1 (True). You can think of it as a reversed C-style comma operator.
 
=={{header|REXX}}==
Line 2,462 ⟶ 2,464:
<pre>what,is,the;meaning,of:life. -> what,si,the;gninaem,of:efil.
we,are;not,in,kansas;any,more. -> we,era;not,ni,kansas;yna,more.</pre>
 
=={{header|Scala}}==
<lang Scala>import scala.io.Source
Line 2,606 ⟶ 2,609:
 
The only difference between the two coroutines (apart from the different names used when flipping back and forth) is the timing of the write of the character with respect to the recursive call.
 
=={{header|TUSCRIPT}}==
{{incorrect|Run BASIC|You are supposed to read characters one by one and not store them in arrays.}}
10,339

edits