Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Remove heads from subexpressions
ResourceFunction["BeheadAt"][expr,pos] returns a beheaded expr at pos. | |
ResourceFunction["BeheadAt"][expr,pos,def] replaces subexpressions without parts with def. | |
ResourceFunction["BeheadAt"][pos] represents an operator form that can be applied to an expression. |
Behead the expression at a given position:
In[1]:= | ![]() |
Out[1]= | ![]() |
Behead at multiple positions:
In[2]:= | ![]() |
Out[2]= | ![]() |
Behead at subhead position to return Sequence[]:
In[3]:= | ![]() |
Out[3]= | ![]() |
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
Provide a different expression instead of Sequence[]:
In[6]:= | ![]() |
Out[6]= | ![]() |
Beheading at subhead position results in Sequence[…]:
In[7]:= | ![]() |
Out[7]= | ![]() |
Use integer positions:
In[8]:= | ![]() |
Out[8]= | ![]() |
Use Unevaluated to prevent premature evaluation:
In[9]:= | ![]() |
Out[9]= | ![]() |
Use operator form:
In[10]:= | ![]() |
Out[10]= | ![]() |
BeheadAt also works inside held arguments:
In[11]:= | ![]() |
Out[11]= | ![]() |
In most cases, BeheadAt is equivalent to using Delete and FlattenAt or ReplacePart with Sequence:
In[12]:= | ![]() |
Out[12]= | ![]() |
In[13]:= | ![]() |
Out[13]= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
In[15]:= | ![]() |
Out[15]= | ![]() |
ReplacePart will not work deeper than the first level inside held arguments:
In[16]:= | ![]() |
Out[16]= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
Delete and FlattenAt replace subheads with Sequence instead of beheading:
In[18]:= | ![]() |
Out[18]= | ![]() |
In[19]:= | ![]() |
Out[19]= | ![]() |
This is equivalent to BeheadAt with Sequence:
In[20]:= | ![]() |
Out[20]= | ![]() |
Trying to behead a nonexistent subexpression generates a message but still evaluates:
In[21]:= | ![]() |
Out[21]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License