Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: [mg5161] Programming: Replace Heads
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Yokoi Takahisa

Posts: 4
Registered: 12/7/04
Re: [mg5161] Programming: Replace Heads
Posted: Nov 9, 1996 2:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

My solution is
% //. (head:_) [args___] :> List[args] /; (List =!= head)

See below:
========================================================
In[188]:=
Clear[a,b,c,d,e,f,g]
f := ({a,b,c,d,e,g}[[Random[Integer, {1,6}] ]])
Array[ Hold@f, {3,2,3},1, Hold@f]//ReleaseHold

% //. (head:_) [args___] :> List[args] /; (List =!= head)

Out[190]=
d[c[a[b[1, 1, 1], e[1, 1, 2], e[1, 1, 3]],
b[a[1, 2, 1], e[1, 2, 2], a[1, 2, 3]]],
e[b[e[2, 1, 1], g[2, 1, 2], g[2, 1, 3]],
e[b[2, 2, 1], g[2, 2, 2], e[2, 2, 3]]],
c[e[g[3, 1, 1], e[3, 1, 2], c[3, 1, 3]],
g[e[3, 2, 1], a[3, 2, 2], a[3, 2, 3]]]]

Out[191]=
{{{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}},
{{1, 2, 1}, {1, 2, 2}, {1, 2, 3}}},
{{{2, 1, 1}, {2, 1, 2}, {2, 1, 3}},
{{2, 2, 1}, {2, 2, 2}, {2, 2, 3}}},
{{{3, 1, 1}, {3, 1, 2}, {3, 1, 3}},
{{3, 2, 1}, {3, 2, 2}, {3, 2, 3}}}}
========================================================

At 1:34 96/11/06 -0500, Xah Lee wrote:
Lee>Problem:
Lee>Given an abritrary expression, how to replace all heads to List?
Lee>
Lee>my solution:
Lee>Map[ {(Sequence@@#)}&, expr, {0,-2}]
Lee>
Lee>Example:
Lee>Clear[a,b,c,d,e,f,g]
Lee>f := ({a,b,c,d,e,g}[[Random[Integer, {1,6}] ]])
Lee>Array[ Hold@f, {3,2,3},1, Hold@f]//ReleaseHold
Lee>Map[ {(Sequence@@#)}&, %, {0,-2}]

* Yokoi Takahisa <yokoi@skk.is.tohoku.ac.jp>
** Graduate School of Information Science,
*** Tohoku Univ. <Tonpei>, Sendai, Japan








Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.