       Re: Very very basic question about Mathematica expressions

• To: mathgroup at smc.vnet.net
• Subject: [mg111230] Re: Very very basic question about Mathematica expressions
• From: David Bailey <dave at removedbailey.co.uk>
• Date: Sat, 24 Jul 2010 05:07:49 -0400 (EDT)
• References: <i2btct\$bd4\$1@smc.vnet.net>

```On 23/07/10 12:11, Bill Rowe wrote:
> On 7/22/10 at 5:44 AM, sam.takoy at yahoo.com (Sam Takoy) wrote:
>
>> 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"?
>
> No, s is a symbol which evaluates to x+h as a result of the
> assignment operation.
>
>> 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"
>
> With s=x+h, x+h is evaluated and becomes one of the OwnValues of
> s. With s[x_, h_]:= x+h, x+h is not evaluated and the expression
> x+h becomes one of the DownValues for s. That is:
>
> In:= s = x + h;
> {OwnValues[s], DownValues[s]}
>
> Out= {{HoldPattern[s] :>  h + x}, {}}
>
> In:= Clear[s];
> s[x_, h_] := x + h
> {OwnValues[s], DownValues[s]}
>
> Out= {{}, {HoldPattern[s[x_, h_]] :>  h + x}}
>
>> Manipulate expects a "Function" so that answers my question.
>
> No, the following works just fine.
>
> Manipulate[Plot[x (1 + a x), {x, 0, 6}], {a, 0, 2}]
>
> Here, the argument x (1 + a x) is simply an expression, clearly
> not a function with named arguments.
>
>> Then what is
>
>> s[h_] := x + h?  Is it an "Expression" or a "Function" of h with a
>> parameter x?
>
> Actually, I would call this an example of bad programming style.
> The result depends on h which is local to s and x which is a
> global parameter. The dependence on a global parameter often
> leads to unexpected results and difficult debugging.
>
>> 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?
>
> Manipulate isn't restricted to functions.
>
>> 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"?
>
> No, x+h doesn't become a function simply by making it an
> argument to Plot.
>
>> 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.
>
> I don't have much to say here. I've been using Mathematica since
> version 1.2 and still find I need to try some things out before
> I know whether they will work as I want. This is particularly
> true of newer features like Manipulate that I don't use frequently.
>
>

Sam Takoy,

I'd just like to add one point to this. All expressions have a FullForm
in which all operators and other syntactic niceties (such as list
notation) are replaced by functions. You can examine the FullForm of an
expression either after or before evaluation:

f[{1, 2, 3 + 4}] // FullForm

f[List[1,2,7]]

f[{1, 2, 3 + 4}] //Hold // FullForm

Hold[f[List[1,2,Plus[3,4]]]]

Any expression can be written in FullForm if you prefer, and when things
go wrong, the answer is often to be found in the FullForm of an expression.

Perhaps you should think of this as the underlying grammar of Mathmeatica.

David Bailey

http://www.dbaileyconsultancy.co.uk

```

• Prev by Date: Re: A Question About Directive
• Next by Date: Re: Very very basic question about Mathematica
• Previous by thread: Re: Very very basic question about Mathematica expressions
• Next by thread: Animated Gif