MathGroup Archive 2000

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

Search the Archive

Re: How to call by reference for particular argument(s)?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22119] Re: How to call by reference for particular argument(s)?
  • From: "David Bailey" <db at salford-software.com>
  • Date: Wed, 16 Feb 2000 02:34:35 -0500 (EST)
  • Organization: University of Salford, Salford, Manchester, UK
  • References: <8889e1$c4r@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Wen-Feng Hsiao <d8442803 at student.nsysu.edu.tw> wrote in message
news:8889e1$c4r at smc.vnet.net...
> Dear all,
>
> I cannot figure out how to call by reference for particular arguments.
> Say, I want to design a function, which will return the position list
> where the second list locates in the first list. For example,
>
> If one inputs the following code:
>
> In[1]:=
> res={};
> myPosition[{a, b, c, d, e}, {b, d}, res];
> res
>
> It outputs {2, 4}. Since the b and d in the second list are in the
> positions 2 and 4 of first list. My code is listed below.
>
> SetAttributes[myPosition, HoldAll];
> myPosition[v1_, {e1_, e2___}, res_Symbol] := Module[{tmp},
>       tmp = Flatten[Position[v1, e1]];
>       If[Length[tmp] != 0,
>         AppendTo[res, tmp[[1]]]];
>       If[nil[{e2}] =!= nothing, (* if e2 not null *)
>         myPosition[v1, {e2}, res]]];
>
> The difficulties are when I change it to the followings, it cannot work:
>
> In[10]:=
> res={};
> u1={a, b, c, d, e}; u2={b, d};
> myPosition[u1, u2, res];
> res
>
> the myPosition[u1, u2, res] cannot be resolved. How can I do? Change the
> signature to myPosition[v1_Symbol, {e1_Symbol, e2___Symbol}, res_Symbol]
> does not work either. It's obvious that the intended call by reference is
> only for the third argument "ref", so please show me the rope, thanks!

The easiest answer is to move the res argument to be the first. Then you can
give myPosition the attribute HoldFirst. In that way the other arguments
will get evaluated before your function is called.

David Bailey
Salford Software




  • Prev by Date: Re: ASCII ... plus new stuff
  • Next by Date: Re: Simple Problem (I suppose...)
  • Previous by thread: Re: How to call by reference for particular argument(s)?
  • Next by thread: Re: How to call by reference for particular argument(s)?