home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.apl
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!sfsuvax1.sfsu.edu!emclean
- From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
- Subject: Re: $: (self-reference) difficulties
- Message-ID: <1993Jan25.034608.10315@csus.edu>
- Keywords: J
- Sender: news@csus.edu
- Organization: San Francisco State University
- References: <abalje47.727925543@ursa>
- Date: Mon, 25 Jan 1993 03:46:08 GMT
- Lines: 43
-
- NB. I don't know why your example doesn't work, it should.
- NB. On Jv6.2 I get the same results as you. Obviously if you change
- NB. $: to factorial then you will have no problems.
-
- a=. '$.=. 2-0=y.' ; '1'
- b=. 'y. * factorial y.-1 '
- factorial=. (a,<b) : ''
- factorial 5
- 120
-
- NB. That the example doesn't work may have something to do with
- NB. the fact that factorial is an explicit verb. This tacit verb
- NB. for factorial using $: works fine.
- NB.
- factorial =. 1:`(]*$:@<:) @. *
- factorial 4
- 24
-
- NB. How does this work?
- NB.
- NB. Well, * used as a monad is signum, returning 1 for positive numbers,
- NB. 0 for 0 and _1 for negative numbers.
- NB.
- NB. @. (agenda ) is a case statment. If factorial is called with a noun
- NB. called "n" then
- NB.
- NB. if "* n " is postive then factorial multiplies n times factorial
- NB. with the argument "<: n" ( n minus 1).
- NB.
- NB. if "* n" is zero then 1 is returned (1: is a verb always returning
- NB. one.)
- NB.
- NB. Similarly changing $: to test in the simplified example yeilds :
-
- b =. 'test 0'
- test =. (a,<b) : ''
- test 2
-
- NB. which works fine.
-
- NB. And the tacit solution using $: works OK too.
-
- test =. 1:`($:@<:@]) @. *
-