cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
hogi
Level XI

Extract Expr

I am a big fan of Eval(Substitute(Expr(...), Expr(..), Expr(.)))

: )

 

The only disadvantage: the Expr(...), Expr(..), Expr(.)

... which brought me to the idea of subst() - substitute() 2.0  ... a function which doesn't evaluate it's arguments.


While playing around with Expression Handling, I noticed that the desired logic for Subst() is quite close to the one of Extract Expr(): no need to use Expr(), and Name Expr() is applied wherever it is possible:

Names Default to here(1);
// direct mode, no Expr necessary
Extract Expr(x^2 + y, x);// -> x
Extract Expr(x^2 + y, c);// no c in x^2 + y ->  empty

// Name Expr mode
c=Expr(x^2);
Extract Expr(x^2 + y , c);// looks up c = x^2, find "x^2"" in x^2 +y

Wonderful!

 ... till:

Extract Expr(cc * d , cc); // -> cc
cc=Expr(x^2);
Extract Expr(cc * d, Expr(cc));// looks up cc = x^2 , so doesn't find cc in cc * d anymore

Dangerous ...


after defining cc as a symbol with a name expr, is it still possible to use c "literally" in Extract Expr

Extract Expr(cc * d , Expr(cc)); // doesn't work
Extract Expr(Expr(cc) , Expr(cc)); // finds Expr(cc)
Extract Expr(cc , Name Expr(Expr(cc))); // -> empty
Extract Expr(Name Expr(Expr(cc)) , Name Expr(Expr(cc))); // finds Name Expr(Expr(cc)), argh!

 

even:

cc=Expr(x^2);
clear symbols(cc);
Extract Expr(cc * d , cc); //->  empty()
1 REPLY 1
hogi
Level XI

Re: Extract Expr

maybe:

use a custom function as a wrapper for Extract Expr

Names default to here(1);
dummy=Function ({expression, pattern},
Extract Expr(expression, pattern);
);

cc = Expr(x^2);
dummy( Expr(cc+d), Expr(cc)); // -> cc