MathGroup Archive 1992

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

Search the Archive

elegance vs. 'quick and dirty': a Mathematica expression beauty contest

  • To: mathgroup at
  • Subject: elegance vs. 'quick and dirty': a Mathematica expression beauty contest
  • From: gaylord at
  • Date: Fri, 3 Apr 1992 02:49:02 -0600

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


#2 and #3 have been proposed by their authors as being 'quick and dirty'. I
don't agree at all with the authors. I rank these expressions in terms of
'elegance in the order
#2, #3, #1

I think that the definition is #2 is by far the 'best-looking' expression
because it is very easily understood and explainable to almost anyone. It
also makes excellent use of list manipulation (Join, First), higher-order
functions (Map) and anonymous functions (..#..&) and especially because it
makes use of the fact that  M is comprised of expressions (Head, Apply).

I have alot of problems when I have to explain or even try to understand
myself an M expression that has  'Hold' in it. It almost always looks like
a 'work-around' or kludge.

I certainly do not want everyone to write M expressions in the same way
(one of the delights of M is that everything can be written almost every
way) and i learn the most about M from reading the varied ways of doing the
'same thing' as in the above example.

The question I want to raise is: how do people prefer their M expressions
to look?

Which of the above expressions do you like and why? 

  • Prev by Date: RE: Division of multi-var polynomials
  • Next by Date: Re: why won't this work??
  • Previous by thread: Re: why won't this work??
  • Next by thread: Re: elegance vs. 'quick and dirty': a Mathematica expression beauty contest