MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replace Heads

  • To: mathgroup at
  • Subject: [mg5231] Re: Replace Heads
  • From: Xah Lee <xah at>
  • Date: Fri, 15 Nov 1996 03:33:49 -0500
  • Organization: Best Internet Communications
  • Sender: owner-wri-mathgroup at

This is a summary on solutions to a programming problem "Replace Heads".

Given an abritrary expression, how to replace all heads to List?

f := ({a,b,c,d,e,g}[[Random[Integer, {1,6}] ]])
expr = Array[ Hold at f, {3,2,3},1, Hold at f]//ReleaseHold
Map[ {(Sequence@@#)}&, expr, {0,-2}]

The following people

David B. Wagner" <dbwagner at>
Allan Hayes <hay at haystack>
Robert Villegas <villegas at>
Wouter <vdmcc at>
fransm at (Frans Martens)

Gave the simple solution:

Apply[ List, expr, {0,-2}]


Yokoi Takahisa <yokoi at>
expr //. (head:_) [args___] :> List[args] /; (List =!= head)

jpk at (Jens-Peer Kuska)
expr //. any_?(List=!=# &)[args___] :> List[args]

Robby Villegas took the problem one step futher and considered cases
where expr contain heads that are not atoms, or whether you want your
expr remain unevaluated. Robby Villegas rank among the tops who
understand Mathematica language in DEPTH. Read his messages.

 xah at; 74631.731 at
 Mountain View, CA, USA

  • Prev by Date: WORD WRAP disable??
  • Next by Date: Re: labelling contours
  • Previous by thread: WORD WRAP disable??
  • Next by thread: Re: reading/writing pictures