MathGroup Archive 2006

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

Search the Archive

Re: Re: returning variable number of arguments from a Module[ ]

On 11/21/06, David Bailey <dave at> wrote:

> bd satish wrote:
> > Hi ,
> >
> >          Is there any method to return a variable number of arguments
> from a
> > Module[ ] or Block[ ] depending upon the o/p list ?
> >
> > For example, consider a module
> >
> >           f[x_,y_]:= Module[ {t,s} , t=x+y; s = t*y  ]
> >
> > Also  assume that , the variable 's' has higher priority than 't' . So
> when
> > a user types a single o/p variable i.e.
> >
> >  W = f[x,y]                then W should have the same value as 's' in
> the
> > module
> >
> > { W, Y } = f[x,y]        then W points to 's' and 'X' points to 't'
> >
> > If I try to do the above ,I'm getting some warning messages..
> >
> > Please help.
> >
> >
> Hello,
> I found it tough to understand this question - so it is possible that I
> have misunderstood you,

but the simple answer is that f[x,y] will be
> evaluated before the assignment of which it is the RHS.

 Hey thanks. I didnt know this fact.

> Also, in your
> module, there is no point in including the variable s - you might as
> well put the expression t*y as the final value.

This is just an example. I didnt exactly want 't' and 's' in that way. They
can be any two values
 returned by Module[ ]

> If f always returns a list of three objects (say) you could always trim
> that subsequently as required as part of the assignment:
> {W,Y}=Take[f[x,y],2];

 But I think this is an additional overhead on the part of f[x,y] . Say
 f[x,y] returns 10 variables {X1, X2 ,.. X10} . Let these variables be
computed inside Module[ ] independently of each other.  If f[x,y] could
somehow "sense" that it needs to return only first two values X1 and X2
(assigned to W and Y respectively), why should f[x,y] burden itself with
computing the values X2,X3..X10 ?

> To be honest, I suspect that there is a better way of solving whatever
> it is that you are trying to do - why not tell us more?

So here I try to put it in a better way:

f[x_,y_]: = Module[ {}, (* compute some values say X1, X2..X10 . All are
assumed scalars*) ]

If I invoke the Module[ ]

 list1 = f[x,y]

then I would like to know if f[x,y] can return Length[list1] values only .
That is, if I make assignments

 { W, Y} = f[x,y]      then f should return W=X1 and Y=X2. f[x,y] should not
                              compute X3,..X10 because it is
supposed to return only 2 values.

{W, Y ,Z } = f[x,y]    then f should return W=X1, Y=X2 and Z = X3. f[x,y]
should not
                              compute X4,X5..X10 because it is supposed
to return only 3 values

In other words, I want f[ ] to "adapt" itself to return only so many values
as the length of LHS list.
That is, f[x,y] should observe the length of LHS and based on this value,
compute only so many required values.

f[ ] should first "hold" itself from evaluating (before assignment) so that
it observes the number of o/p arguments & then evaluates only as many needed

  • Prev by Date: Re: symbolic solution of ODE
  • Next by Date: Re: Numerical Integration
  • Previous by thread: Re: returning variable number of arguments from a Module[ ]
  • Next by thread: Re: returning variable number of arguments from a Module[ ]