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

MathGroup Archive 2011

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

Search the Archive

Re: execution model: Function vs. delayed execution

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121341] Re: execution model: Function vs. delayed execution
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 11 Sep 2011 07:29:49 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On 9/10/11 at 7:30 AM, alan.isaac at gmail.com (Alan) wrote:

>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?

The code

Function[x, (x=DeleteDuplicates[x];x)][{1,1,2}]

has x playing two roles, as a formal argument to the function
and to hold the result. Mathematica attempts to evaluate the
function body by replacing x with the {1,1,2} wherever x
appears. This results in attempting to evaluate

{1,1,2}=DeleteDuplicates[{1,1,2}]

which is what generates the error message. Adding a new variable
to the code, i.e.,

Function[x, (y=DeleteDuplicates[x];y)][{1,1,2}]

eliminates the problem and error message. Or more simply,

Function[x, DeleteDuplicates[x]][{1,1,2}]

Obviously, this last eliminates the redundancy which you state
was intentional.





  • Prev by Date: How create new style sheet- Mathematica 8
  • Next by Date: Re: Column vectors should be interpreted as simple lists where
  • Previous by thread: Re: execution model: Function vs. delayed execution
  • Next by thread: Re: execution model: Function vs. delayed execution