0

The following makes sense to me:

In[1] = f[a, b] /. f[x__] -> x
Out[1] = Sequence[a, b]

In[2] = f[a, b] /. f[x__] -> {x} Out[2] = {a, b}

However, the following don't make sense:

In[3] = f[a, b] /. f[x__] -> Length[{x}]
Out[3] = 1

In[4] = f[a, b] /. f[x__] -> Total[{x}] Out[4] = Sequence[a,b]

I was expecting the following: Out[3] = 2 and Out[4] = a + b, especially considering the value of Out[2]. Am I missing something here? Is there some subtle nuance of BlankSequence that I have missed?

I'm using Mathematica desktop 13.2.1.0 on Debian 11.

user64494
  • 26,149
  • 4
  • 27
  • 56
xdavidliu
  • 181
  • 6

1 Answers1

0

It's because -> evaluates the right hand side of the rule eagerly when the rule is defined, not when it's later applied. In the case of In[3], we have

In[3] = f[a, b] /. f[x__] -> Length[{x}]

Before applying the rule, the RHS Length[{x}] is evaluated to 1. Then we replace x with Sequence[a,b] in the expression 1, which still is just 1.

Similarly, in In[4], the expression Total[{x}] is first evaluated to just x, and then after replacement, we get Sequence[a,b].

xdavidliu
  • 181
  • 6