MathGroup Archive 2000

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

Search the Archive

Re: A simple programming question.

  • To: mathgroup at
  • Subject: [mg23206] Re: [mg23174] A simple programming question.
  • From: Carl Woll <carlw at>
  • Date: Mon, 24 Apr 2000 01:12:13 -0400 (EDT)
  • Organization: Physics Department, U of Washington
  • References: <>
  • Sender: owner-wri-mathgroup at

Hi Jack,

Since I'm basically responsible for that piece of code, I'll try to give
you an explanation. First concentrate on the heart of the code:

i[n_] := (i[n] = Null; n);

To make things more transparent, I'll replace Null with Nothing (note
that in my original version of this function I used Sequence[], since
the Sequence's will automatically disappear). Now, suppose that the
parentheses were not there. Then the above code turns into two

i[n_] := i[n] = Nothing;

The second statement n; does nothing now. Let's see what happens with
the first statement.

i[n_] := i[n] = Nothing

After executing the above, let's see what kind of downvalues i has.

?? i

i[n_] := i[n] = Nothing

Just what we expect. Now, let's apply the function to something:



The output is Nothing. Let's see what kind of downvalues i has now.

?? i

i[1] = Nothing

i[n_] := i[n] = Nothing

We see that i has the additional downvalue i[1]=Nothing. Hence, the next
time i gets the argument 1, it will use this downvalue directly without
using the delayed set function definition. Let's contrast this to what
happens when the parentheses are there.

i[n_] := (i[n] = Nothing; n)

?? i

i[n_] := (i[n] = Nothing; n)

So far everything proceeds as before. Applying i to 1,



we get the output 1. When given the argument 1, the function i carries
out the command


The compound statement first defines a new downvalue, and then executes
1. The result of this compound statement is whatever the output of the
final command is. In this case the output is 1, which is why the output
above is 1. Let's see what kind of downvalues i has now

?? i

i[1] = Nothing

i[n_] := (i[n] = Nothing; n)

Again, the next time i gets the argument 1, it will use the first
downvalue above to immediately return Nothing. The only time i returns
something other than nothing is the very first time its given a
particular argument. This is great if we want to go through a list, and
replace repeated occurrences of something with nothing, leaving the
first occurrence alone. This is exactly what OrderedUnion was supposed
to do.

As far as a scheme to help you in understanding this kind of code, in
addition to the ones you mentioned, you could use ?? or DownValues[],
and break up the code to smaller pieces and analyze the smaller pieces.
Of course, a little practice in analyzing code snippets like this should
make things much easier.

Hope that helps.


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?
> 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

Carl Woll
Dept of Physics
U of Washington

  • Prev by Date: Re: fastest way to do pair-sum / make pair-list
  • Next by Date: Re: Gaussian fit
  • Previous by thread: A simple programming question.
  • Next by thread: Re: Re: A simple programming question.