to write a program that swapped two or more variables without using an intermediate variable. I replied with:
#!/usr/bin/perl
use strict;
print "Variable a: "; chomp(my $a = <STDIN>);
print "Variable b: "; chomp(my $b = <STDIN>);
print "Variable c: "; chomp(my $c = <STDIN>);
print "Variable d: "; chomp(my $d = <STDIN>);
print "Variable e: "; chomp(my $e = <STDIN>);
print "a = $a\n";
print "b = $b\n";
print "c = $c\n";
print "d = $d\n";
print "e = $e\n";
($a, $b, $c, $d, $e) = ($e, $d, $c, $b, $a);
print "\nSwapped\n\na = $a\nb = $b\nc = $c\nd = $d\ne = $e\n";
To which I was told:
Doesn't count , you used an intermeadit variable , you placed
them into a anonomous array befor you swaped them . ($a,$b) is
the same as _@ = ($a,$b);
sorry , try again !
I told him:
By that reasoning, no programming language on earth can do it.
All programming languages makeup some sort of a spot in memory
to do the swap.
Perl just happens to give it a name.
He sais back to me:
Well , Tim asked me the same challenge , and after pondering it
for a wile I had to tell him Perl couldn't do it just because of
the use of an intermitant var. I think the languages HE says can
do it "REBOL" also use an intermitant var , they just don't
document it ! so In a sence your right , now we just got to get
Tim to admit the other languages also process the swap using
an unseen VAR and he is just not aware of it !
What do you all think? |