RE: A simple programming question.

• To: mathgroup at smc.vnet.net
• Subject: [mg23194] RE: [mg23174] A simple programming question.
• From: "David Park" <djmp at earthlink.net>
• Date: Mon, 24 Apr 2000 01:12:03 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Jack,

The parentheses are necessary because both statements are part of the
definition on i[n_]. If the parentheses are missing, then only the first
statement, i[n]==Null, is the definition. The n; would be left dangling and
not really do anything.

This is an example of "Dynamic Programming" which is discussed in Section
2.4.9 (Functions That Remember Values They Have Found) of the Mathematica
Book. When i is mapped onto the list, then the first time it encounters some
specific value of n, say n == s, it returns s, but it also defines a new
value for i[s] which is Null - and remembers it. After that, whenever it
encounters i[s], it returns Null. The routine attains its efficiency at the
expense of building up a structure of i definitions.

In Woll's original routine, instead of using i[n]==Null, he uses
i[n]==Sequence[]. That simply eliminates the duplicate items from the list.
I changed the routine to Null, because of the special requirements of the
Towle problem, where the list we are operating on is derived from the
original list and we must go back and eliminate the duplicates from the
original list. If you are operating with lists of exact elements (integers
and symbols), then it is better to use Sequence[] if your object is to
eliminate duplicates.

David Park

> From: Jack Goldberg [mailto:jackgold at math.lsa.umich.edu]
To: mathgroup at smc.vnet.net
> 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?
>
> 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?
>
> Someone care to guide me here?  Thanks,
>
> Jack

```

• Prev by Date: Re: Help, my MathLink Examples segfault.
• Next by Date: Re: Gaussian fit
• Previous by thread: Re: A simple programming question.
• Next by thread: Re: A simple programming question.