From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
Subject: Re: "shut up and deal the cards"
X-Xxdate: Sat, 26 Dec 92 05:13:50 GMT
X-Useragent: Nuntius v1.1.1d13
Organization: University of Illinois
Date: Sat, 26 Dec 1992 11:08:07 GMT
X-Xxmessage-Id: <A7619A8E2C019B12@mm-mac18.mse.uiuc.edu>

Subject: Re: "shut up and deal the cards"
From: David Petry, petry@zermelo.math.washington.edu
Date: 25 Dec 92 00:12:34 GMT

In article <1992Dec25.001234.21234@u.washington.edu> David Petry,
petry@zermelo.math.washington.edu 

compares my

Transpose[Sort[Transpose[{Table[Random[],{Length[lis]}], lis}]]][[2]]

with his

randomize[L_] := Module[{a,b,k,p=L}, 
			For[k=Length[L],k>1,k--,
				b=Random[Integer,{1,k}];
				a=p[[k]];
				p[[k]]=p[[b]];
				p[[b]]=a];
			p]

and finds the first program to be five times faster than the second.  He
concludes that Mathematica programmers must learn a bunch of slick tricks
to write reasonably efficient programs.  

================

A slick programming trick is simply  a technique that has not yet been
mastered. Once you do, it becomes a  programming tool that can be used in
many seemingly disparate situations. 

eg., about two months ago, i was working on a  computer simulation known
as 'invasion percolation' (a model for flow through a porous medium and
also of cancer spread) in which there is a list of lattice sites having
associated random numbers (representing the resistance of a lattice site
to being selected [eg., the susceptability of a cell to attack by a
virus])  and one has to select the site with the lowest random number
(eg., the most vulnerable cell). 

the way i finally did it was to sort the list {..{Random[], {a,b}..} and
take the second part of the first element in the sorted list. 

In  other words,  i used precisely the same technique there as in the
topmost program here (though i didn't recognize that my invasion
percolation program could be used to randomize a list as well) . 

Of course, in the invasion percolation case, it was not a trick at all
but was a very direct, straightforward implementation of the site
selection process in the simulation 

 [btw - this method will appear in my next Computer Simulations column in
"Mathematica in Education" as an improved version of the 'spreading
phenomena' program in my column a couple of issues ago]. 

a final note: i found David's procedural program very difficult to
decipher (i had to spend time figuring out all of the assignment
satements) while i was able to write my functional program in a  direct
way by simply implementing what i wanted to do with the entire list using
 built-in functions (one key to functional programming is to deal with a
list as a single data object without unnecessarily deconstructing it) [i
also orignally wrote it as an anonymous function].

many people may not find 'functional' thinking to be obvious to do but
that's primarily  because they haven't learned how to do it (to that
extent functional programming does appear to be a trick to the person
unfamiliar with it).  its really a very natural way to think. it would
help if there were a book on how to learn to do functional programming
[there will be by august].
Newsgroups: sci.math.symbolic