On Finding conjugate pairs in MMa, on pattern matching, on functional programming
- To: mathgroup at yoda.physics.unc.edu
- Subject: On Finding conjugate pairs in MMa, on pattern matching, on functional programming
- From: gaylord at ux1.cso.uiuc.edu
- Date: Wed, 1 Jul 1992 07:04:57 -0500
================================================== furry at hitl.washington.edu Jeff James's program (which i assume works) >CCSort[items_] := Flatten[ > Sort[items, N[ Re[#1] <= Re[#2] ] &] //. > ({a___,l_,b___,r_,c___} :> {a,If[ Im[l] > Im[r], {l,r}, {r,l} ],b,c} > /; Length[{a,b,c}] > 0 && l == Conjugate[r]) >] to sort a list of complex numbers such that they are ordered by real part and conjugate pairs follow each other in the list eg., CCSort[{1+I,0,1+I,0,1-I,1-I}] {0, 0, 1 + I, 1 - I, 1 + I, 1 - I} fully qualifies as a functional program (nested function calls, anonymous functions, pattern matching) while pattern matching is not a necessary requirement for functional programming, it is a part of any good functional language, such as Standard ML, M (the Mathematica programming language) and Haskell. Jeff's program therefore satisfies the David's request: David Jacobson (jacobson at cello.hpl.hp.com) >>Anyone who believes that nonfunctional programming is dysfunctional is >>hereby urged to post a functional solution. ---------------------------------------------- as a side note: this particular problem highlights the incredible usefulness of pattern matching. recall the problem of unnesting that was discussed here a few months ago lis = {{a},{b,b},{a,a},{{{b,b,b},{b}}}, {a,a},{a},{{b,b},{b,b}}}; Map[(# //.{x__List}->x)&, lis] {{a}, {b, b}, {a, a}, {b, b, b}, {b}, {a, a}, {a}, {b, b}, {b, b}} and also the famous runEncode problem RunEncode[x_List] := Map[({#, 1})&,x]//.{u___,{v_,r_},{v_,s_},w___}-> {u,{v,r+s},w} ----------------------------- i might (and will) note that by comparision, other computer algebra systems (CAS's) which also support functional programming (such as Macsyma and Maple [since no one on the net wanted to discuss the functional features of the Maple language, I finally had to learn it myself and it does appear to support functional programming]) are weaker than M in terms of pattern-matching capabilities [in fact, M is also a more sophisticated pattern-matcher than any of the conventional functional languages]. richard j. gaylord, university of illinois, gaylord at ux1.cso.uiuc.edu "if you're not programming functionally, then you must be programming dysfunctionally" richard j. gaylord, university of illinois, gaylord at ux1.cso.uiuc.edu "if you're not programming functionally, then you must be programming dysfunctionally"