Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Special thanks!

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23240] Special thanks!
  • From: Jack Goldberg <jackgold at math.lsa.umich.edu>
  • Date: Sat, 29 Apr 2000 22:04:48 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Group!

I am indebted to David Parks, Carl Woll, Wijnand Schepens and Preston
Nichols for their clear and thoughtful responses to my post, wherein I
asked for an explanation of this simple (?) code:

OrderedUnion2[li_] := Module[ {i},
	i[n_] := ( i[n] = Null; n );
	i /@ li ]

I know that there were others besides myself who were puzzled by line 2,
the definition of i[n_].  I now understand my confusion and in the hope
that this helps others, I offer it here.
	An expression such as 

fnt[x_] := (expr1; expr2; expr3); 

when CALLED evaluates each expression inside the () but returns only
expr3. In our case, what is returned is  n,  the argument of  i[n].
Once  i[n_] is called say with n = 2.3  then  i[2.3] = Null is constructed
AND  2.3 is returned. When (and if)  i[2.3] is encountered again, Mathematica 
searches its definition list (the downvalues for  i and finds i[2.3]=Null 
before it finds i[n_] := (.....)  because  i[2.3] is more specific than the 
general formula for  i[n_].  So the second line of the code will not  "fire" 
when the argument of  i  is  2.3  except (as noted) the first time 2.3 is
seen.  For example, 

OrderedList2[{1,1,1,1,1,1}]
 
calls line 2 only once, the first time it sees  i[1].  The last 5 times
it sees i[1] it uses the definition  i[1]=Null which it constructed BUT
DID NOT return the first time  and only time  i[n_] in line 2 is called.

!! In other words  i[1] = Null in line 2 is NOT called ever again!!

At the risk of "beating a dead horse", here is a modification of 
Woll/Parks code that convinced me.

test[li_] := Block[ {i, j=0},
	i[n_] :=(i[n]=Null; j=j+1);
	i/@li
		]

This modification enables us to counts the number of times  i[n_] is
called. We can see that i[n_] is called only once for the list
{1,1,1,1,1,1,1} but 6 times for the list  {3,2,99,8,3,1}. 
   

If this explanation is wrong then I really don't know whats going on :-)

Jack








  • Prev by Date: Re: Please help with a Hypergeometric2F1 problem...
  • Next by Date: Re: Please help with a Hypergeometric2F1 problem...
  • Previous by thread: Re: pure functions
  • Next by thread: Re: ...can't help, but let me muddy the waters a bit. ;-) ...