[Jprogramming] identifying the calling function

Dan Bron j at bron.us
Thu Jan 27 01:30:29 HKT 2011


Ian wrote:
>  Hands up who understands ~help/dictionary/d212.htm

$: is used for (anonymous) recursion. For example, where you might've
written:

	   fact =: * fact@:<:^:(1&<) 

	   fact 5
	120

using $:, you could've omitted the verb-naming step (which is incidental to
its function, or at least you'd like that to be true):
		   
	  ( * $:@:<:^:(1&<) ) 5
	120
	   
but note: $: refers to the _longest_ verb that contains it.  So whereas:

	   factMas2  =:  2 + fact
	   factMas2 5
	122

vs:

	   (2 +  ( * $:@:<:^:(1&<) ))  5
	532	

All of a sudden, $:'s scope has broadened to include the 2&+ (on _every_
invocation, including the recursive ones).  Of course, you could fix this by
manually limiting the scope of  $:

	   fact1 =:  * $:@:<:^:(1&<)
	   fact1Mas2 =: 2 + fact1

	   fact1Mas2 5
	122
   	        
... but that kind of defeats the purpose a bit.  Also, if anyone ever
decides to apply  f.  and it hits your  $:-verb, it'll fix you good:

	
	   fact1Mas2 f. 5  NB. Theoretically identical to line above
	3

There are ways around this (e.g.  2 + 3 : '( * $:@:<:^:(1&<) ) y' )  but
none is entirely satisfying.

-Dan

PS:   f.  should wrap all verbs in parens before quoting them in an explicit
context.





More information about the Programming mailing list