MathGroup Archive 2001

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

Search the Archive

Re: = or := ???

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30687] Re: = or := ???
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 8 Sep 2001 02:22:31 -0400 (EDT)
  • References: <9n18cn$26b$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Oliver,

Here are some suggestions and comments.

1: PROBLEM WITH %

Provided that the input at expr_ will not contain # the following works

    function[expr_,list_] :=
        {{expr /.#/.zzz->list[[1]]},
         {expr /.#/.zzz->list[[2]]},
         {expr /.#/.zzz->list[[3]]}
        }&[{xxx->yyy}

But if the input contains # then the following sort of probem occurs (I
simplify for clarity)

    fn1[expr_]:=(expr/.#)&[x->a]

    fn1[{x,#}]

        {a,a->a}

When we probably wanted

    {x,#}/.x->a

        {a,#1}

The following are safer

    fn2[expr_]:=
    With[{rl=x->a},(expr/.rl)]
    fn2[{x,rl}]

        {a,rl}

    fn3[expr_]:=
    Function[rl,(expr/.rl)][x->a]
    fn3[{x,rl}]

        {a,rl}

The reason is that, before {x,rl} is passed to expr, there is a renaming to

    With[{rl$=x->a},(expr/.rl&)]
    Function[rl$,(expr/.rl$)][x->a]

After the passing we have

    With[{rl$=x->a},({x,rl}/.rl&)]
    Function[rl$,({x,rl}/.rl$)][x->a]

2. PROBLEM WITH list

We can require that the list must be a list with three elements:

    function[expr_,list_List/;Length[list]==3] :=
        {{expr /.#/.zzz->list[[1]]},
         {expr /.#/.zzz->list[[2]]},
         {expr /.#/.zzz->list[[3]]}
         }&[{xxx->yyy}]

3. PROBLEM RE := or =

The above suggestions worked with :=.
If we use = then the right side of the setting is evaluated before a rule is
stored.
Leaving aside the problem with list not at this stage having three elements
the evaluation would proceed to work on the present expr, picking up any
current value..
For example

    expr= presentexpr[x];

    fn4[expr_]=Function[rl,(expr/.rl)][x->a]

        presentexpr[a]

4. A WAY TO DEAL WITH #.

(fn5[expr_]:=(expr/.#))&[x->a]

    fn5[{x,#}]

        {a,#1}

Here is what we stored

Definition[fn5]

        fn5[expr_] := expr /. x -> a
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Oliver Friedrich" <oliver.friedrich at tz-mikroelektronik.de> wrote in message
news:9n18cn$26b$1 at smc.vnet.net...
> Hallo,
>
> I'm in trouble with defining a function (in fact at the moment it works
the
> way I wanted, but with horrible workarounds).
>
> I did some computations which led me to a list rule1={xxx->yyy} with a
long
> expression.
>
> The next is, that I want to set up a function where expr is modified by
> rule1 and then by rule2
>
> function[s_,list_] :={{expr /.% /.zzz->list[[1]]},{expr /.%
> /.zzz->list[[2]]},{expr /.% /.zzz->list[[3]]}}
>
> Now for the traps: I want to use rule1 with the % because I don't want to
> give that Output a name (never need it again). Now if I define function
with
> Set, I get the error that PartSpecification list[[_]] is longer than depth
> of object (I Know what this error means, no problem about this).
> If I define with SetDelayed, I can't use my % notation, because function
is
> evaluated repeatedly from within any point in my sessions, and I can't
rely
> on % anymore.
> It should work with Set, if I could tell Mathematica that the list_
argument
> in function represents a list. How can this be done (define the "type" of
of
> what a symbol stands for?). I played around with some Hold[] stuff, but
> that's not the way, I want to have it.
>
> Any help appreciated !
>
> Oliver Friedrich
>
>
>
>
>
>




  • Prev by Date: Re: Collect with Simplify destroys Hold
  • Next by Date: Good news from the Math Forum@Drexel
  • Previous by thread: Re: = or := ???
  • Next by thread: Re: = or := ???