Hi Tom!

On Don, 19 Okt 2006, Tom Phoenix wrote:
> >$b = (++$a) + ($a++);
> 
> I'm not sure whether C does so, but I believe that Perl does NOT

No, also C does not guarantee any predescribed order. It depends on the
compiler.

BUT: Even if we consider *both* ways of evaluation the expression tree,
we arrive at 8:

Lets take the following evaluation: Let 
        e1      (++$a)
        e2      ($a++)
        e3      e1 + e2
we evaluate first e1 and obtain for e1 the value 4 (as it is an
pre-increment), a is now also 4.
Then we evaluate e2: ($a++) which evaluates to 4, too, because this time
it is an post-increment.
Evaluation e3: 4+4 gives 8.

If we do the other ordering:
        e1      ($a++)
        e2      (++$a)
        e3      e1 + e2
we evaluate first expression 1: $a++, which gives 3 (and a is 4 as it is
a post-increment). Then we evaluate e2: which gives 5 (and a is also 5),
so wie have 3 + 5 which is also 8.

I only see an explanation when the pre-increment *NOT*ALWAYS* returns
the value *pre increment.

OR: the expression tree is not actually evaluated in some sub-tree
order, but in some leave up order. Then we would have
        e1      ++$a
        e2      $a++
        e3      (++$a)
        e4      ($a++)
        e5      e3 + e4
which would yield:
        e1      4       pre increment, a=4
        e2      4       a=5
        e3      4
        e4      5       it returns the current value of a
        e5      9
(the above interpretation is very unclear, but it is the only way I can
see an explanation)
But this would be extremely counter-intuitive!!!

> Did this come up in a real-world situation, or were you specifically
> seeking to test the limits of Perl? In other words, what problem, if
> any, are you trying to solve?

Teaching Perl to students, and to get them to understand the difference
between pre and post increment.

Best wishes

Norbert

-------------------------------------------------------------------------------
Dr. Norbert Preining <[EMAIL PROTECTED]>                    Università di Siena
Debian Developer <[EMAIL PROTECTED]>                         Debian TeX Group
gpg DSA: 0x09C5B094      fp: 14DF 2E6C 0307 BE6D AD76  A9C0 D2BF 4AA3 09C5 B094
-------------------------------------------------------------------------------
Now it is such a bizarrely improbable coincidence that
anything so mindboggingly useful could have evolved purely
by chance that some thinkers have chosen to see it as the
final and clinching proof of the non-existence of God.
The argument goes something like this: `I refuse to prove
that I exist,' says God, `for proof denies faith, and
without faith I am nothing.'
The Babel fish is a dead giveaway, isn't
it? It could not have evolved by chance. It proves you
exist, and so therefore, by your own arguments, you don't.
QED.'
                 --- Douglas Adams, The Hitchhikers Guide to the Galaxy

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to