Message Boards Message Boards

0
|
7138 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Does there exist a Rule that performs instead of being a replacement rule?

Posted 5 years ago

To clarify the question, consider this. I'm not asking for something that is just an "If then" statement. I want something similar to rule that matches structure and can also behave like If in the Wolfram language. For the example, I'll simply call the function "When" and it will be similar to ReplaceAll:

In[1] := B = {};
In[2] := When[_Integer*> Append[B, _Integer^2]][{1,2,3,Pi,E,5}]
Out[2] := {1,4,9,25}
In[3] := B
Out[3] := {1,4,9,25}

In the above example, In[2] can be read as "When I see an integer I will append unto B the integer squared". As can be seen the list B is now modified, and these actions aren't exclusively to appending, I'm talking about a function that evaluates code when something is true or when structure is matched, and can even perform other tasks:

In[1] := B = {1,2,3,4,5,Sin[6]};
In[2] := X = 13;
In[3] := C = {}
In[4] := When[{_[u_] *> Append[C, Sin[u+1]],_u>3||_u <= 2 *> Append[C,Cos[u]]][B];
In[5] := C
Out[5] := {Sin[7], Cos[1], Cos[2], Cos[4], Cos[5]}

When evaluating the line In[4] it can be read as "When I see something inside a function I will append unto C Sine of the value inside some function added to 1 and then when a value is less than or equal to two I will append unto C the Cosine of that value. Where the function When threads through the same list twice with the given rules, using *> to specify that it is an action to perform versus an object/structure to replace. This would allow great freedom for Rule based programming considering this will generalize what tasks can be performed and when actions, such as merely appending, replacing, or just giving a "See this? Do this."

POSTED BY: Joshua Champion
5 Replies

Mathematica is an expression rewriting language. Rules and definitions basically say "See this? Rewrite as that.". And then, once it has done the rewrite, it re-evaluates the resulting expression. So, what you want doesn't seem to be anything special. If (shudder), you want the rewritten expression to have side effects, you can arrange it.

Perhaps what you're looking for is RuleDelayed.

POSTED BY: John Doty

Mutating a List is generally a difficult and inefficient way to get things done in Mathematica. I think it's much easier to define a function that yields rewritten list elements as needed, and use the magic Nothing to excise unwanted results. First the case we want:

f[x_Integer] := x^2

Now, a catch-all for the cases we don't want:

f[_] := Nothing

Then, just Map f to your input:

Map[f, {1, 2, 3, Pi, E, 5}]
(*
  {1, 4, 9, 25}
*)
POSTED BY: John Doty

I'm still not clear on exactly what you want but look at Reap and Sow (or related, Throw and Catch). You can use them in a "myWhen" function that triggers your action. Note that you can use the optional extra argument to Reap and Sow to gather up different results in different lists.

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer

Joshua,

I think you want Cases. Modifying their first documentation example a bit:

In[2]:= Cases[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, i_Integer -> i^2]

Out[2]= {1, 1, 4, 9, 81}

Regards,

Neil

POSTED BY: Neil Singer

Thank you for your response Neil, however this isn't what I'm looking for. I'm in need of something that will either look for trueness in the left hand side of the > or a structure match and then evaluate the right hand side of the > that way I can do conditional tasks that partially relate. Again, thank you for taking your time to respond!

POSTED BY: Joshua Champion
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract