Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1992

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

Search the Archive

elegance vs. 'quick and dirty'

  • To: mathgroup at
  • Subject: elegance vs. 'quick and dirty'
  • From: a_rubin at (arthur rubin)
  • Date: Fri, 3 Apr 92 10:31:27 PST

gaylord at 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}}}
        finallist = {{a,a,a},{a,a},{b,b},{b,b,b},{a,a,a,a},{b,b},{b}}

Three solutions have been proposed:


finallist = FixedPoint[
                Replace[#,{A___,List[X___List],B___} :> {A,X,B}]&,

Let $A be an unused or local symbol

finallist = Flatten[List @@ (
                      startlist /. List[X___List] :> $A[X] 
                              /. {List->$A,$A->List}
                   ] /. $A->List

 f[x_List] :=
  Apply[Join,Map[(If[Head[First[#]] === List, #, {#}] &), x]]

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

Consider the following forms:

#2A = 2
 f[x_List] :=
  Apply[Join,Map[(If[Head[First[#]] === List, #, {#}] &), x]]

 f1[X:{A_,___}] := X
 f1[X_List] := {X}
 f[x_List] := Join @@ (f1 /@ x)

Which of those do you consider clearer? 

  • Prev by Date: Re: elegance vs. 'quick and dirty': a Mathematica expression beauty contest
  • Next by Date: Re: bug in Splice?
  • Previous by thread: Re: elegance vs. 'quick and dirty': a Mathematica expression beauty contest
  • Next by thread: Re: bug in Splice?