Odd word problem: Difference between revisions
Content added Content deleted
(Updated to work with Nim 1.4: replace proc "nothing" with a proc type and a variable declaration to get a closure and to allow side effects.) |
|||
Line 1,707: | Line 1,707: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
===Using a closure=== |
|||
{{trans|Python}} |
{{trans|Python}} |
||
<lang nim>import unicode |
<lang nim>import unicode |
||
Line 1,738: | Line 1,739: | ||
while (if e: odd() else: even()): |
while (if e: odd() else: even()): |
||
e = not e</lang> |
e = not e</lang> |
||
Output: |
|||
{{out}} |
|||
<pre>$ echo "what,is,the;meaning,of:life." | ./oddword |
<pre>$ echo "what,is,the;meaning,of:life." | ./oddword |
||
what,si,the;gninaem,of:efil. |
what,si,the;gninaem,of:efil. |
||
echo "we,are;not,in,kansas;any,more." | ./oddword |
echo "we,are;not,in,kansas;any,more." | ./oddword |
||
we,era;not,ni,kansas;yna,more.</pre> |
we,era;not,ni,kansas;yna,more.</pre> |
||
===Using recursion=== |
|||
{{trans|ALGOL 68}} |
|||
<lang Nim>import strutils |
|||
proc reverseWord(ch: var char) = |
|||
var nextch = stdin.readChar() |
|||
if nextch.isAlphaAscii(): |
|||
reverseWord(nextch) |
|||
stdout.write(ch) |
|||
ch = nextch |
|||
proc normalWord(ch: var char) = |
|||
stdout.write(ch) |
|||
ch = stdin.readChar() |
|||
if ch.isAlphaAscii(): |
|||
normalWord(ch) |
|||
var ch = stdin.readChar() |
|||
while ch != '.': |
|||
normalWord(ch) |
|||
if ch != '.': |
|||
stdout.write(ch) |
|||
ch = stdin.readChar() |
|||
reverseWord(ch) |
|||
stdout.write(ch)</lang> |
|||
{{out}} |
|||
Same as with the closure version. |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |