Re: A simple programming question.

*To*: mathgroup at smc.vnet.net*Subject*: [mg23186] Re: A simple programming question.*From*: Wijnand Schepens <Wijnand.Schepens at rug.ac.be>*Date*: Mon, 24 Apr 2000 01:11:57 -0400 (EDT)*Organization*: RUG*References*: <8dokm3$jfu@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Jack Goldberg wrote: > Hi group; > > In a post by Russell Towle [mg23117] appears a short > program written by David Park from an idea of Carl Woll. > Here it is: > > OrderedUnion2[li_] := > Block[ {i}, > i[n_] := (i[n] = Null; n); > i /@ li > ] > > I understand about 98% of what's going on, but one item > keeps eluding me. What is the role of the parentheses in > the first line of the Block? Without () the program fails > and as is, the program inserts Null after the second time > it sees the same number in li. That is, > > {1,1,2,3,4,3} => {1, Null, 2, 3, 4, Null} > > I understand the point of i[n_] := i[n]=Null and I think > I understand the role of the "n" after the ";" - but I > don't quite get the whole picture. Presumably, the point > is not to have Null substituted for all entries in li but > only the duplicates... But what role does () play in all this? The thing is that ";" has lower precedence than ":=". If you leave out the parentheses, then the definition of i reads i[n_]:=i[n] = Null. The "n;" which follows is then just the next statement of your block; it just evaluates the symbol "n" (which presumably yields a number or if n is undefined, echoes "n"), but since it is followed by ";" this output isn't produced. So the "n;" is superfluous (unless "n" is defined to be a program which changes some globale variable, but that is very unlikely) So what happens in the block is this: * define an function i[n_] : everything in parentheses * map this function to the list, i.e. apply it to every element In order to understand how the function i works, you should consult The Mathematica book 2.4.9 on "Functions That Remember Values They Have Found". Basically, i[n_] gives a recipe for doing something with an arbitrary n But, if i[n] is defined for some specific n, then this definition is taken first (because it is more specific, or less general) e.g. i[n_]:=2 n i[3]="hello" Now i[3] evaluates to "hello", and i[2] evaluates to 2*2=4. So in the program above, i[n_]:=(i[n]=Null; n), whenever you feed an "n" for which i[n] is not defined, the two expressions in parentheses are evaluated; the first ones defines i[n] to be Null for this particular value of n, the second one outputs the number n. Now if i[n] is called for a number n for which a particular definition (namely i[n]=Null) already exists, then this definition is used first, and Null is output. > A second related question. In order to understand the > "inner workings" I used Trace to no avail; I looked up > CompoundExpression to no avail; I tried Print[] at > various spots, also to no avail. Can anyone recommend > a scheme that could have helped me understand what was > going on? My experience here is that Trace produces an awfull mess. Print can help in debugging. But the most important thing is to read the Mathematica book (esp. Chapter 2) thoroughly, and try to understand all the simple examples given there. Don't let the difficult stuff in 2.2 and 2.3 scare you away. 2.4 and 2.5 are very interesting. > > Someone care to guide me here? Thanks, > > Jack Hope this was of some help. Feel free to ask more. Wijnand

**Re: Gaussian fit**

**Re: problem with Integrate**

**RE: A simple programming question.**

**output from a package**