Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Mathematica language issues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53018] Re: [mg53002] Re: Mathematica language issues
  • From: DrBob <drbob at bigfoot.com>
  • Date: Mon, 20 Dec 2004 06:34:35 -0500 (EST)
  • References: <200412191114.GAA18018@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

> Surprisingly,
>> Compile[{}, Module[{x = 0}, While[x++, EvenQ[x]]; x]][]
>> also outputs 1.

It's not surprising. The value of x++ is 1 and that's not True, so While exits on the first iteration.

>> Compile[{}, Module[{x = 0},  While[x++; EvenQ[x]]; x]][]
>> outputs 2
>> This indicates EvenQ[x] is being evaluated with the current value of x

In version 5.1, it returns 3. It should return 1, but CompoundExpression is sometimes deeply mysterious!

Bobby

On Sun, 19 Dec 2004 06:14:49 -0500 (EST), Bill Rowe <readnewsciv at earthlink.net> wrote:

> On 12/18/04 at 3:59 AM, ruebenko at imtek.uni-freiburg.de (Oliver
> Ruebenkoenig) wrote:
>
>
>> Dear Maxim,
>
>>> All the typical issues with the Mathematica programming language
>>> are still present in version 5.1:
>
>>> Compile[{},
>>>   Module[{x = 0},
>>>     While[x++; EvenQ[x] ];
>>>     x
>>> ]][]
>
>> Compile[{}, Module[{x = 0}, x++; While[EvenQ[x]];
>>      x]][]
>
>> x++; EvenQ[x] is CompoundExpression[ x++, EvenQ[ x ] ]
>
>> While[ test ] needs a test not a CompoundExpression
>
> The usual syntax of While is While[test, expr] but While[test] is also allowed and test can be a compound expression. The issue is clearly related to how Mathematica evaluates the compound expression.
>
> Compile[{}, Module[{x = 0},
>     While[EvenQ[x], x++]; x]][]
>
> outputs 1 as expected
>
> Surprisingly,
>
> Compile[{}, Module[{x = 0},
>     While[x++, EvenQ[x]]; x]][]
>
> also outputs 1. I suspect if test becomes more complex, say a compound expression, deviating from the indicated syntax order won't continue to get the same result as using the expected order.
>
> Note,
>
> Compile[{}, Module[{x = 0},
>     While[x++; EvenQ[x]]; x]][]
>outputs 2
>
> This indicates EvenQ[x] is being evaluated with the current value of x rather than the updated value resulting from x++. However, I still don't understand why Maxim's original code outputs 3. But I also don't see an issue. I am never surprised by unexpected output when I deviate from expected syntax.
> --
> To reply via email subtract one hundred and four
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Redefining the minus operator
  • Next by Date: Re: Re: Mathematica language issues
  • Previous by thread: Re: Mathematica language issues
  • Next by thread: Re: Mathematica language issues