Re: Re: Sequential evaluation of lists
- To: mathgroup at smc.vnet.net
- Subject: [mg53127] Re: [mg53108] Re: [mg53076] Sequential evaluation of lists
- From: DrBob <drbob at bigfoot.com>
- Date: Fri, 24 Dec 2004 05:59:18 -0500 (EST)
- References: <200412220953.EAA04525@smc.vnet.net> <200412231259.HAA21217@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
That's still left to right evaluation:
Print[d]&/@{Print[e]}&/@{Print[f]}//HoldForm//FullForm
HoldForm[Map[Function[Map[Function[Print[d]],List[Print[e]]]],List[Print[f]]]]
The Head is Map, and its arguments are Function[...] and List[Print[f]].
Function holds its arguments and evaluates to
(Print[d] &) /@ {Print[e]} &
without printing anything. Then List[Print[f]] is evaluated to the value {Null}, causing f to print, BTW.
Then the function above is applied to Null. In FullForm, that function is
Function[Map[Function[Print[d]], List[Print[e]]]]
Map[...] is applied to the Function argument (but there is no argument), so Map[...] is simply evaluated.
The first argument of Map is Function[Print[d]], which evaluates to Print[d] & without printing anything.
The second argument of Map is List[Print[e]], which evaluates to {Null} while printing e.
Then Print[d] & is applied to Null, and d prints.
Evaluation was left to right in every expression.
Bobby
On Thu, 23 Dec 2004 07:59:50 -0500 (EST), yehuda ben-shimol <benshimo at bgu.ac.il> wrote:
> The evaluation is not always left to right. There are some cases that it
> goes from right to left, and it SHOULD do so.
> L2R evaluation
> {Print[a], Print[b], Print[c]};
> results with
> a
> b
> c
> R2L evaluation
> Print[d] & /@ {Print[e]} & /@ {Print[f]}
> and get
> f
> e
> d
> {{Null}}
> For your specific case (and others as well) of x=0;{x++,x++,x++} you can
> use Trace to follow the evaluation order
> x=0;Trace[{x++,x++,x++}]
> and get
> {{x++, {x, 0}, {x = 1, 1}, 0}, {x++, {x, 1}, {x = 2, 2}, 1}, { x++, {x,
> 2}, {x = 3, 3}, 2}, {0, 1, 2}}
> or
> Trace[{a=1,b=2,c=3}]
> and get
> {{a = 1, 1}, {b = 2, 2}, {c = 3, 3}, {1, 2, 3}}
> yehuda
> Ray Koopman wrote:
>
>> When I first started using Mathematica (v2), one of the features that
>> I found rather surprising is its sequential evaluation of lists, as in
>>
>> In[1]:= x = 0; {x++,x++,x++}
>> Out[1]= {0,1,2}
>>
>> I had expected a warning that such code should be avoided because
>> it presumed sequential evaluation, which could not be guaranteed,
>> and a recommendation to treat list elements as being evaluated in
>> parallel -- if not simultaneously then in no particular order.
>> However, so far I have found no exception to sequential evaluation
>> and no mention of it in any documentation. Have I missed something?
>>
>>
>>
>
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
- References:
- Sequential evaluation of lists
- From: "Ray Koopman" <koopman@sfu.ca>
- Re: Sequential evaluation of lists
- From: yehuda ben-shimol <benshimo@bgu.ac.il>
- Sequential evaluation of lists