home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / apl / 1370 < prev    next >
Encoding:
Text File  |  1993-01-24  |  1.7 KB  |  56 lines

  1. Newsgroups: comp.lang.apl
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!sfsuvax1.sfsu.edu!emclean
  3. From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
  4. Subject: Re: $: (self-reference) difficulties
  5. Message-ID: <1993Jan25.034608.10315@csus.edu>
  6. Keywords: J
  7. Sender: news@csus.edu
  8. Organization: San Francisco State University
  9. References: <abalje47.727925543@ursa>
  10. Date: Mon, 25 Jan 1993 03:46:08 GMT
  11. Lines: 43
  12.  
  13.    NB. I don't know why your example doesn't work, it should.
  14.    NB. On Jv6.2 I get the same results as you. Obviously if you change
  15.    NB. $: to factorial then you will have no problems.
  16.  
  17.    a=. '$.=. 2-0=y.' ; '1'
  18.    b=. 'y. * factorial y.-1 '
  19.    factorial=. (a,<b) : ''
  20.    factorial 5
  21. 120
  22.  
  23. NB.    That the example doesn't work may have something to do with
  24. NB.    the fact that factorial is an explicit verb. This tacit verb
  25. NB.    for factorial using $: works fine.
  26. NB. 
  27.    factorial =. 1:`(]*$:@<:) @. *
  28.    factorial 4
  29. 24
  30.   
  31. NB.    How does this work?
  32. NB. 
  33. NB.    Well, * used as a monad is signum, returning 1 for positive numbers,
  34. NB.    0 for 0 and _1 for negative numbers.
  35. NB. 
  36. NB.    @. (agenda ) is a case statment. If factorial is called with a noun
  37. NB.    called "n" then
  38. NB. 
  39. NB.    if "* n " is postive then factorial multiplies n times factorial
  40. NB.    with the argument "<: n" ( n minus 1).
  41. NB. 
  42. NB.    if "* n" is zero then 1 is returned (1: is a verb always returning
  43. NB.    one.)
  44. NB.     
  45. NB.    Similarly changing $: to test in the simplified example yeilds :
  46.  
  47.    b =. 'test 0'
  48.    test =. (a,<b) : ''
  49.    test 2
  50.  
  51. NB.   which works fine.
  52.  
  53. NB.   And the tacit solution  using $: works OK too.
  54.  
  55.    test =. 1:`($:@<:@]) @. *
  56.