Odd word problem: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: fixed a stray blank that was in a comment.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 101: Line 101:
we,are;not,in,kansas;any,more.
we,are;not,in,kansas;any,more.
we,era;not,ni,kansas;yna,more.</pre>
we,era;not,ni,kansas;yna,more.</pre>

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
Line 1,735: Line 1,736:
we,era;not,ni,kansas;yna,more.
we,era;not,ni,kansas;yna,more.
</pre>
</pre>



=={{header|Perl}}==
=={{header|Perl}}==
Line 1,821: Line 1,821:
}
}
}</lang>
}</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}}==
=={{header|Phix}}==
Line 2,025: Line 2,001:
true .
true .
</pre>
</pre>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
This example uses recursion.
This example uses recursion.
Line 2,229: Line 2,206:
;; ;; -> we,era;not,ni,kansas;yna,more.
;; ;; -> we,era;not,ni,kansas;yna,more.
</lang>
</lang>

=={{header|Raku}}==
(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}}==
=={{header|REXX}}==
Line 2,462: Line 2,464:
<pre>what,is,the;meaning,of:life. -> what,si,the;gninaem,of:efil.
<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>
we,are;not,in,kansas;any,more. -> we,era;not,ni,kansas;yna,more.</pre>

=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>import scala.io.Source
<lang Scala>import scala.io.Source
Line 2,606: Line 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.
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}}==
=={{header|TUSCRIPT}}==
{{incorrect|Run BASIC|You are supposed to read characters one by one and not store them in arrays.}}
{{incorrect|Run BASIC|You are supposed to read characters one by one and not store them in arrays.}}