Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Re: Re: Assignment problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86429] Re: [mg86383] Re: [mg86362] Re: Assignment problem
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Tue, 11 Mar 2008 02:58:15 -0500 (EST)
  • References: <fqo8o1$suo$1@smc.vnet.net> <fqqs3b$kf5$1@smc.vnet.net> <200803100702.CAA24648@smc.vnet.net>

On 10 Mar 2008, at 08:02, danl at wolfram.com wrote:

>> Helen Read wrote:
>>> Albert Retey wrote:
>>>> Note that usually using a Do-Loop is the better choice in  
>>>> Mathematica:
>>>
>>> (as compared with a For loop).
>>>
>>> This is the second post I've seen today that says that Do loops are
>>> better than For loops in Mathematica. What is better about them  
>>> (aside
>>> from slightly simpler syntax)?
>>
>> If it were up to me, I would ban the For construct from the  
>> Mathematica
>> language.  We wouldn't loose anything by eliminating For.
>
> "We" is a bit too general. Me, I'd lose a lot.
>
>
>> 1.  It is completely redundant.[...]
>> 2.  When newcomers to the language want a looping construct, the  
>> first
>> thing they search for is "for", because this is what they are  
>> familiar
>> with from other languages.  This results in abominations like this:
>>
>> l = {};
>> For[i = 1, i <= 10, i++,
>>     AppendTo[l, f[i]];
>> ]
>> [...]
>> 3. For[] usually results in slow code, mainly because assignment (and
>> changing the values of variables) is slow in Mathematica.[...]
>> 4.  Using For[] is error-prone.
>>
>> When using For[], one is forced to create an otherwise useless global
>> variable, "i".[...]
>> Another possiblity of error is to just run the For[] loop without the
>> initialization l = {}, which will create unexpected results with no
>> error or warning message.
>>
>> 5.  Once a newcomer has found For[], s/he might just stick to it,
>> thinking that it is "the main looping construct".[...]
>
> All of these are good points: they indicate ways in which For[] is  
> often
> used to disadvantage.
>
>
>> I think that For[] is completely useless for experienced users (in  
>> the
>> sense that its absence would not make a difference), but it can cause
>> considerable damage when newbies start using it.  I don't  
>> understand at
>> all why it was included in the language.[...]
>
> If it were to go away, believe me, I would miss it.
>
>
>> Maybe it was just a marketing
>> decision, so those coming from other languages will feel more at  
>> home,
>> and Mathematica can be advertised as a multiparadigm programming  
>> system
>> (which it is even without For!).  I don't think that a C programmer  
>> who
>> is transliterating from C to Mathematica will feel at home in
>> Mathematica ...
>>
>> Note that I am *not* arguing against the use of a procedural style in
>> Mathematica.  In fact, a procedural approach is the best solution to
>> *certain* problems.  But when newbies find For[] they tend to not  
>> look
>> further and not learn the style of programming which makes  
>> Mathematica
>> really effective.  For[] also brings out most of the disadvantages of
>> using a procedural style in Mathematica: bad performance because of
>> constantly changing variables; too much verbosity and a too "low- 
>> level"
>> approach to problems; and leaking of temporary variables into the  
>> global
>> namespace (because of the way scoping works in Mathematica).
>
> There are procedural constructs for which For[] just seems well  
> suited to
> handle. At least that's how it seems to arise in code I write. This  
> might
> be from a C-and-related background, I'm not sure. I doubt there is a  
> "one
> size fits all" approach to procedural programming, so maybe it's a
> construct that suits some individual styles and not others.
>
> I guess I should make full disclosure so I will mention that I have  
> even
> used Goto/Label in Mathematica. Not often; five times in 16+ years.  
> But
> that's probably five more times than virtually everyone else.
>
> Daniel Lichtblau
> Wolfram Research
>

I have seen many cases of your using the For loop but I think in all  
cases it was within code that used Compile. Using For with Compile  
makes the main objection to it (poor performance) irrelevant so then  
it all becomes a matter of taste.
I suppose that is also the case with Goto/Label but I confess I have  
never used it and can only remember one case of someone doing so. That  
person by the way was the great mathematician and economist, Nobel  
Prize winner and hero of a well known film, John Nash. I am not sure  
what that tells us about this issue, except that indeed it does not  
seem to be the case that "one size fits all".

Andrzej Kozlowski


  • Prev by Date: Re: FindFit issue
  • Next by Date: Re: A question regarding a hyperbolic geometric function
  • Previous by thread: Re: Re: Assignment problem
  • Next by thread: Mathematica 6 EPS output