Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.1K Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Break up an expression into two levels at a time?

Suppose I have an expression

test = a[b[e[g, h], f], c, d];

I like to break it up into { a[b,c,d], b[e,f], e[g,h] }

This is a start

Cases[test, _[__], {0, Infinity}]

{e[g, h], b[e[g, h], f], a[b[e[g, h], f], c, d]}

but I haven't figured out how to use Level to get the result I want.

POSTED BY: Frank Kampas
2 Replies

Thanks.

POSTED BY: Frank Kampas

Maybe

In[4]:= Replace[#, head_[__] :> head, 1] & /@ Level[test, {-Infinity, -2}]

Out[4]= {e[g, h], b[e, f], a[b, c, d]}
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard