elegance vs. 'quick and dirty'
- To: mathgroup at yoda.physics.unc.edu
- Subject: elegance vs. 'quick and dirty'
- From: a_rubin at dsg4.dse.beckman.com (arthur rubin)
- Date: Fri, 3 Apr 92 10:31:27 PST
gaylord at ux1.cso.uiuc.edu writes: a recent discussion of Flattening dealt with going from startlist = {{a,a,a},{a,a},{{b,b},{b,b,b}},{a,a,a,a},{{b,b},{b}}} to finallist = {{a,a,a},{a,a},{b,b},{b,b,b},{a,a,a,a},{b,b},{b}} Three solutions have been proposed: #1 finallist = FixedPoint[ Replace[#,{A___,List[X___List],B___} :> {A,X,B}]&, startlist] Let $A be an unused or local symbol finallist = Flatten[List @@ ( startlist /. List[X___List] :> $A[X] /. {List->$A,$A->List} ) ] /. $A->List #2 f[x_List] := Apply[Join,Map[(If[Head[First[#]] === List, #, {#}] &), x]] #3 f[{x__}] := Sequence[x] /; Not[ And @@ AtomQ /@ {x} ] f[{x__}] := {x} f /@ startlist ========================== He prefers them in the order, #2,#3,#1. The first thing to do is to make sure they work. (As writer #1, I admit that my second form doesn't work.) The second form under #1, and form #3 don't work (at least on my implementation of Mma 2.0.) My attempt under form #1B was to use the built-in function Flatten, and form #1A was an attempt to implement the problem as rule-based programming (which I didn't notice at first); it should have been: f[{A___,List[X___List],B___}] := {A,X,B} f[X_ ] := X f[startlist] Consider the following forms: #2A = 2 f[x_List] := Apply[Join,Map[(If[Head[First[#]] === List, #, {#}] &), x]] #3A f1[X:{A_,___}] := X f1[X_List] := {X} f[x_List] := Join @@ (f1 /@ x) Which of those do you consider clearer?