       Re: a question about [[ ]]

• To: mathgroup at smc.vnet.net
• Subject: [mg46629] Re: a question about [[ ]]
• From: drbob at bigfoot.com (Bobby R. Treat)
• Date: Thu, 26 Feb 2004 17:53:18 -0500 (EST)
• References: <c1h028\$9p6\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Possibly Ted Ersek's HoldTemporary package will satisfy. You can find
that at MathSource:

http://library.wolfram.com/infocenter/MathSource/705/

Needs["Ersek`HoldTemporary`"]
Clear[f, g]
f[{x_, y_}] := {f[x], f[y]}
g[x_] /; Length[f[x]] > 1 := {f[x][], f[x][]}
g[x_] := HoldTemporary[Take[f[x], 2]]

(* or g[x_] := HoldTemporary[{f[x][], f[x][]}] *)

g[a]
{f[a][], f[a][]}

g[{x, y}]
{f[x], f[y]}

f[a_] := {a, a^2}
g[a]
{a, a^2}

g[{x, y}]
{{x, x^2}, {y, y^2}}

There's not a lot you can DO with g[a] while f[a] is undefined, but
HoldTemporary has options that may help.

Bobby

Paolo Bientinesi <pauldj at cs.utexas.edu> wrote in message news:<c1h028\$9p6\$1 at smc.vnet.net>...
> Hello,
> here is what I am trying to do:
>
> g is a function that invokes f.
> f[x] is undefined,
> while f[{x,y}] returns a list, say {f[x],f[y]}  (or any other list).
>
>
> Assume
> f[{x_, y_}] := { f[x], f[y] }
> and
> g[x_] := {f[x][], f[x][]}
>
>
> I would like to have
>
> In := g[a]
> Out:= {f[a][], f[a][]}
>
> and
>
> In := g[{b,c}]
> Out:= {f[b], f[c]}
>
>
> Rationale: function g has the knowledge that f iv evaluated return a list.
> If f is not evaluated I want the symbolic expression to be returned.
>
>
> The problem is the first case: g[a].
> I cannot prevent Mathematica from evaluating f[a][]
> returning the unwanted a.
> For f[a][] the situation is slightly better because after the mesg
> Part::"partw": "Part \!\(2\) of \!\(f[a]\) does not exist."
> Mathematica return the unevaluated expression f[a][].
>
>
> Basically I would like Mathematica to evaluate (Something)[[ ]]
> only is Something is a list and leave everything symbolic otherwise.
>
>
> Thanks for any suggestion.

```

• Prev by Date: Re: Question about Integrate and If
• Next by Date: RE: Matrix Element Extraction