Re: Very very basic question about Mathematica

*To*: mathgroup at smc.vnet.net*Subject*: [mg111222] Re: Very very basic question about Mathematica*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Sat, 24 Jul 2010 05:06:17 -0400 (EDT)

Just a correction to my previous reply: During normal evaluation subexpressions are evaluated before expressions ( I wrote expressions before subexpressions, which is incorrect - this only happens if evaluation of a particular (sub)expression is non-standard). Regards, Leonid On Fri, Jul 23, 2010 at 12:17 AM, Leonid Shifrin <lshifr at gmail.com> wrote: > Hi Sam, > > A short answer is that there are no functions in Mathematica, only > expressions and > replacement rules (except pure functions perhaps). When you define a > "function" like > > s[x_, h_]:=x+h > > you really associate a certain global replacement rule with a symbol <s>. > When > evaluator sees a match, it rewrites according to this rule. There are no > stack frames > or activation records - it is a completely different mechanism that > superficially resembles > function calls (there is evaluation stack which is similar but not the > same). So, <s> > is always a Symbol. > > Generally, Mathematica at its hart is a rule-based engine, not a functional > language. Functional > layer sits already on top of it. So comparisons with Scheme or Lisp may be > misleading. In > particular, if you study Mathematica evaluation loop, you will see that the > recursive part will be similar > to Lisp (that expressions are evaluated before subexpressions normally), > but the part related > to rule application is not found in Lisp.You may want to check out some > simple account explaining > these things about Mathematica, perhaps some parts of the virtual book or > documentation related to evaluation of expressions. I also dwelt somewhat on > this in my book, > > www.mathprogramming-intro.org/ > > Regarding Manipulate, this behavior is related to scoping (Manipulate is a > scoping > construct and you relationship between <s> and <h> was originally defined > outside > of the scope of Manipulate). > > Hope this helps. > > Regards, > Leonid > > > > On Thu, Jul 22, 2010 at 1:44 PM, Sam Takoy <sam.takoy at yahoo.com> wrote: > >> Thanks to all who responded! >> >> May I belabor this point a little. I understand how to make Manipulate >> work and I understand functions. (I am a product of Scheme from with >> Mathematica seems to have borrowed a few ideas.) >> >> My question is much more formal: What are the building blocks of >> Mathematica, the formal language. When you say >> >> s = x+h >> >> what is s? >> >> Is it an "expression"? Does s represent x+h wherever it appears >> (assuming x and h are unassigned at the time of s=x+h)? Apparently not >> always: yes in s/.h->5, but not in Manipulate. >> >> So here, then is my "model" of Mathematica: >> In s = x+h, s is an "Expression" >> In s[x_, h_]:=x+h, s is a "Function" >> >> Manipulate expects a "Function" so that answers my question. >> >> Then what is >> >> s[h_] := x + h? Is it an "Expression" or a "Function" of h with a >> parameter x? >> >> Would then Manipulate[Plot[s[h], {x, 0, 1}, PlotRange -> {0, 1}], {h, 0, >> 1}] work? (The answer is yes.) So apparently, Plot is happy with an >> "Expression", but Manipulate wants a "Function"? Why? >> >> Also, in Manipulate[Plot[x+h, {x, 0, 1}, PlotRange -> {0, 1}], {h, 0, >> 1}], x+h is no longer an "Expression", but is once again a "Function", >> because of the context? Even though it's inside Plot which is happy with >> an "Expression"? >> >> A personal note: I guess I'm a little frustrated that after a few months >> of working with Mathematica, I still have to try things before I know >> whether they'll work or not. I'm used to having a clear picture of the >> grammar of the language that I'm working with, but I'm struggling here. >> >> >> On 7/21/2010 7:14 AM, dr DanW wrote: >> > I ran into this problem yesterday. I don't know exactly why it >> > happens, I think it has something to do with the way Manipulate >> > localizes variables. To solve it, I use a trick I found that lets me >> > take an expression built up of global symbols and localize the >> > symbols. Your trivial example: >> > >> > s = x + h >> > >> > Make a function out of it. The Evaluate[] is necessary to evaluate s, >> > which replaces it with x+h >> > >> > sfnc = Function[{x, h}, Evaluate[s]] >> > >> > Now the Manipulate[] works fine >> > >> > Manipulate[Plot[sfnc[x, h], {x, 0, h}], {h, 0.1, 1}] >> > >> > I find myself using this trick a lot. >> > >> > Regards, >> > Daniel >> > >> >> >> >