OK - not extended arithmetic...

Having been keen on direct definition in APL for many years,  I've just started looking back at the late John Scholes' description of his Dfns in Dyalog APL,  to be found at
   https://www.dyalog.com/uploads/documents/Papers/dfns.pdf

I tried coding a couple of simple recursive examples as DDs in J, namely sieve (Primes) and fac(torial):

sieve =: {{)v
'' sieve y
:
nxt =. {. y
msk =. 0~: nxt | y
if. */ }. msk do.
   x, y
else.
   (x,nxt) sieve msk#y
end.
}}

fac =: {{)v
   1 fac y
:
if. y = 0 do.
   x
else.
   (x * y) fac y - 1
end.
}}

They work fine:
   sieve 2}.i.21
2 3 5 7 11 13 17 19
   fac 8
40320

BUT - Dfns allows self-reference of anonymous functions with the delta symbol,  ∇, which _might_ display ok here.  SO,  where I've got "sieve" or "fac" within the dyadic sections of these definitions,  John had ∇ (delta).   (He allowed ambivalent functions by having an optional
default assignment of ⍺, alpha, ie x for J users.)

However, replacing (x * y) fac y - 1 by   (x * y) $: y - 1 results in,  eg:
   fac 8
|stack error: fac
|   (x*y)    $:y-1

Is this my poor understanding of $:   - I don't use it much -  or is it an instance of the problem with $: that Henry noted in "Re: [Jbeta] Error with $:" on 11/Oct/20,  or something else?

Thanks again,

Mike

PS - In case the APL displays reasonably well,  here are copies of these 2 Dyalog Dfns:
(The line numbers are not part of the definitions.)
Lines sieve[4]  and factorial[2] are examples of "Guards" which are sort of
if. test then.  value return. end.

      sieve←{             ⍝ Sieve of Eratosthenes.
[1]        ⍺←⍬             ⍝ Default no primes yet.*
[2]        nxt←1↑⍵         ⍝ Next prime, and
[3]        msk←0≠nxt|⍵     ⍝ ... mask of non-multiples.
[4]        ∧/1↓msk:⍺,⍵     ⍝ All non multiples - finished.
[5]        (⍺,nxt)∇ msk/⍵  ⍝ Sieve remainder.
[6]    }

      factorial←{     ⍝ Tail recursive factorial.
[1]        ⍺←1 ⍝  *
[2]        ⍵=0:⍺
[3]        (⍺×⍵)∇ ⍵-1
[4]    }

* examples of default value of left argument if applied monadically.



--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to