Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #4852 > unrolled thread
| Started by | Alan <alan.isaac@gmail.com> |
|---|---|
| First post | 2011-09-10 11:34 +0000 |
| Last post | 2011-09-12 08:25 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
execution model: Function vs. delayed execution Alan <alan.isaac@gmail.com> - 2011-09-10 11:34 +0000
Re: execution model: Function vs. delayed execution David Bailey <dave@removedbailey.co.uk> - 2011-09-11 11:31 +0000
Re: execution model: Function vs. delayed execution "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-11 11:34 +0000
Re: execution model: Function vs. delayed execution Alan <alan.isaac@gmail.com> - 2011-09-12 08:25 +0000
| From | Alan <alan.isaac@gmail.com> |
|---|---|
| Date | 2011-09-10 11:34 +0000 |
| Subject | execution model: Function vs. delayed execution |
| Message-ID | <j4fhvn$2gr$1@smc.vnet.net> |
Warning: I am a Mathematica newbie and not a CS type, so my vocabulary may prove clumsy.
I am used to a deferred execution model of function definition. Roughly, if I can write code that would be successfully executed outside a function definition, then I can make it a function body by appropriately "wrapping" it.
In Mathematica, I can evaluate the following):
x = {1,1,2}
x=DeleteDuplicates[x]; x
(Note: the redundancy is intentional.)
Next, I attempt to "wrap" this as follows
Clear[x]
Function[x, (x=DeleteDuplicates[x];x)][{1,1,2}]
This produces an error:
Set::shape: "Lists {1,1,2} and {1,2} are not the same shape."
Can you help me understand the execution model that leads to this?
Thanks,
Alan Isaac
[toc] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2011-09-11 11:31 +0000 |
| Message-ID | <j4i67c$bvl$1@smc.vnet.net> |
| In reply to | #4852 |
On 10/09/2011 12:34, Alan wrote:
> Warning: I am a Mathematica newbie and not a CS type, so my vocabulary may prove clumsy.
>
> I am used to a deferred execution model of function definition. Roughly, if I can write code that would be successfully executed outside a function definition, then I can make it a function body by appropriately "wrapping" it.
>
> In Mathematica, I can evaluate the following):
> x = {1,1,2}
> x=DeleteDuplicates[x]; x
> (Note: the redundancy is intentional.)
>
> Next, I attempt to "wrap" this as follows
> Clear[x]
> Function[x, (x=DeleteDuplicates[x];x)][{1,1,2}]
>
> This produces an error:
> Set::shape: "Lists {1,1,2} and {1,2} are not the same shape."
>
> Can you help me understand the execution model that leads to this?
>
> Thanks,
> Alan Isaac
>
>
The problem is that in your original version x is a global variable,
whereas in the second, it is the formal argument to your function - so
you end up executing:
{1,1,2}=DeleteDuplicates[{1,1,2}]
The difference as compared with other computer languages that you may
have used - e.g. C or Fortran - is that there x would be the name of a
variable that holds the argument, rather than the argument itself - as
it is in Mathematica.
The ultimate reason for this difference, is that Mathematica is designed
to solve both symbolic and numerical problems. So for example, you might
want to write a function that takes a variable as argument that has no
value. This would result in an operation on an undefined variable in C
or Fortran.
David Bailey
http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-09-11 11:34 +0000 |
| Message-ID | <j4i6d3$c0m$1@smc.vnet.net> |
| In reply to | #4852 |
On Sat, 10 Sep 2011 12:34:15 +0100, Alan <alan.isaac@gmail.com> wrote:
> Warning: I am a Mathematica newbie and not a CS type, so my vocabulary
> may prove clumsy.
>
> I am used to a deferred execution model of function definition.
> Roughly, if I can write code that would be successfully executed outside
> a function definition, then I can make it a function body by
> appropriately "wrapping" it.
>
> In Mathematica, I can evaluate the following):
> x = {1,1,2}
> x=DeleteDuplicates[x]; x
> (Note: the redundancy is intentional.)
>
> Next, I attempt to "wrap" this as follows
> Clear[x]
> Function[x, (x=DeleteDuplicates[x];x)][{1,1,2}]
>
> This produces an error:
> Set::shape: "Lists {1,1,2} and {1,2} are not the same shape."
>
> Can you help me understand the execution model that leads to this?
>
> Thanks,
> Alan Isaac
>
Function employs strict lexical scoping that overrides the HoldFirst
attribute of Set:
In[1] :=
Function[x, x = DeleteDuplicates[x]; x][{1, 1, 2}] // Trace
Out[1] =
{
Function[x, x = DeleteDuplicates[x]; x][{1, 1, 2}],
{1, 1, 2} = DeleteDuplicates[{1, 1, 2}]; {1, 1, 2},
{
{
DeleteDuplicates[{1, 1, 2}],
{1, 2}
},
{1, 1, 2} = {1, 2}, (* <- this gives rise to the message *)
{
Message[Set::shape,{1, 1, 2},{1, 2}],
{Set::shape,Lists `1` and `2` are not the same shape.}
<< extra tracing omitted >>
}
}
}
this is documented via the statement,
"Function has attribute HoldAll. The function body is evaluated only after
the formal parameters have been replaced by arguments."
Using With for comparison:
In[2] :=
With[{x = {1, 1, 2}},
x = DeleteDuplicates[x]; x
]
During evaluation of In[2] :=
Set::shape: Lists {1, 1, 2} and {1, 2} are not the same shape.
Out[2] = {1, 1, 2}
This is in contrast to Block which implements dynamic scoping (or indeed
Module, whose scoping is effectively lexical, but not as directly so as
that of With or Function):
In[3] :=
Block[{x = {1, 1, 2}},
x = DeleteDuplicates[x]; x
]
Out[3] = {1, 2}
[toc] | [prev] | [next] | [standalone]
| From | Alan <alan.isaac@gmail.com> |
|---|---|
| Date | 2011-09-12 08:25 +0000 |
| Message-ID | <j4kflh$kga$1@smc.vnet.net> |
| In reply to | #4865 |
On Sunday, September 11, 2011 7:34:59 AM UTC-4, Oleksandr Rasputinov wrote:
> Function employs strict lexical scoping that overrides the HoldFirst
> attribute of Set:
>
> In[1] :=
> Function[x, x = DeleteDuplicates[x]; x][{1, 1, 2}] // Trace
[snip]
> this is documented via the statement,
>
> "Function has attribute HoldAll. The function body is evaluated only after
> the formal parameters have been replaced by arguments."
*Very* helpful! I did read that documentation more than once,
but now I understand what it is saying.
Thanks!
Alan Isaac
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web