Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Yokoi Takahisa Posts: 4 Registered: 12/7/04
Posted: Nov 9, 1996 2:01 AM

My solution is

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

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