[Date Index]
[Thread Index]
[Author Index]
Re: Re: Sequential evaluation of lists
On Dec 23, 2004, at 7:58 AM, János wrote:
> It would be nice to have all List manipulation functions have an option
> Direction->Left or Direction->Right, just as Limit has +1 or -1. I even
> would like functional programming functions like Map or Apply have this
> option. Any seesaw type operation modeling would greatly benefit from
> it and nature has many.
MapIndexed can be used to map a function whose result depends on the
distance from the first or last position in a list, MapIndexed can
also be used to make a version of Apply that depends on the position in
the list.
>
> On Dec 22, 2004, at 4:53 AM, 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?
>>
++ is not a functional operation by definition since it alters the
value of its argument. It is explicitly a procedural operation and
using it explicitly assumes a known order of evaluation. The
functional equivalent of your statement that assumes no order of
evaluation and thus no knowledge of global state beyond the arguments
to the function is
x=0;#+1&/@{x,x,x}
Of course the existence of a globally accessible x and the ability to
redefine its value through Set also breaks the functional programming
paradigm, but Mathematica has never claimed to be a purely functional
language, only to allow it as one of a variety of styles. At any rate,
given that Mathematica is not dogmatic about functional programming,
and that ++ is explicitly not a functional operation there is no need
for a warning because no rule is being broken. Referring to the help
browser, ++ is described in section 2.5.4 of The Mathematica Book which
is titled "Special Forms of Assignment" which instructs the programmer
that this is not functional programming since assignment of state is
not allowed. Furthermore the first phrase of said section is
"Particularly when you write procedural programs in Mathematica,..."
again informing the user that the assumptions of functional programming
do not apply.
I believe ++ can be implemented in purely functional languages like
Haskell using a construct called a monad but I don't really understand
the details and since Mathematica is not purely functional it's not
necessary to include the monad model anyway.
Regards,
Ssezi
Prev by Date:
**Re: Re: Mathematica language issues**
Next by Date:
**Re: Need your help to solve a PDE.**
Previous by thread:
**Re: Re: Sequential evaluation of lists**
Next by thread:
**Re: Sequential evaluation of lists**
| |