Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1992
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*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 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? 







  • 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?