Re: Re: Assignment problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg86383] Re: [mg86362] Re: Assignment problem*From*: danl at wolfram.com*Date*: Mon, 10 Mar 2008 02:02:19 -0500 (EST)*References*: <fqo8o1$suo$1@smc.vnet.net> <fqqs3b$kf5$1@smc.vnet.net>

> 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

**Follow-Ups**:**Re: Re: Re: Assignment problem***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>