Message Boards Message Boards

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

Newbie Wolfram Language Question

Posted 5 years ago

Hello. I'm having a little difficulty understanding the behavior of the Characters function when I apply a set of rules to it. Let me explain. I can apply it like I have below to create lists of the constituent characters in a string in two different ways producing the same output as shown below. In[1]:= a=Characters["fish"] b="fish" // Characters Out[1]= {"f", "i", "s", "h"} Out[2]= {"f", "i", "s", "h"}

Both ways of applying the function give the same output. In[3]:= a==b Out[3]= True

However, when I apply a rule to both expressions above, only one takes the change. In[4]:= Characters["fish"]/. {"f" -> "d"} "fish" // Characters/. {"f" -> "d"} Out[4]= {"d", "i", "s", "h"} Out[5]= {"f", "i", "s", "h"}

If I apply the same rule to the variables set at the top, the rule is applied in both cases. In[6]:= a/. {"f" -> "d"} b/. {"f" -> "d"} Out[6]= {"d", "i", "s", "h"} Out[7]= {"d", "i", "s", "h"}

My question is shouldn't "fish"//Characters/.{"f"->"d"} produce {d,i,s,h}? And if not, what is it that I am missing or not getting?

I have attached this post as a notebook as well.

Thanks. Philip.

Attachments:
POSTED BY: Philip Cowan
4 Replies
Posted 5 years ago

The nuts and bolts:

In[1]:= Precedence[Postfix]
Out[1]= 70.

In[2]:= Precedence[ReplaceAll]
Out[2]= 110.
POSTED BY: Hans Milton
Posted 5 years ago

Thank you to both of you for your prompt answers.

POSTED BY: Philip Cowan
Posted 5 years ago

Hi Philip,

As Claudio mentioned, you need to use parenthesis to override the default precedence. /. has a higher precedence than //. The precedence of all operators is documented here.

POSTED BY: Rohit Namjoshi

If you put parentheses to stipulate the order of operations this works.

("fish" // Characters) /. {"f" -> "d"}

(* {"d", "i", "s", "h"} *)

POSTED BY: Claudio Chaib
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