bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-apl] Doing without loops


From: Kacper Gutowski
Subject: Re: [Bug-apl] Doing without loops
Date: Mon, 31 Aug 2015 08:51:42 +0200

On Mon, Aug 31, 2015 at 2:19 AM, Louis de Forcrand <address@hidden> wrote:
> Now, my two questions aren't necessarily bugs, but the
> blog is titled "bugs and discussion". Anyway, how would I go about
> repeating a function using ⍣ until the answer reaches a certain value?
> e.g.:
>         FIB ← {  2 ↓ ( { ⍵ , +/ ¯2 ↑ ⍵ } ⍣ ⍵ ) 0 1 }
>         FIB 5
> 1 2 3 5 8

(...)
> I keep wanting to do something like:
>
>        while ( ( ¯1 ↑ FIB i ) < 1000) {
>              i++;
>        }
>        FIB i
>
> but I know I can't.
> It's not elegant anyway :)

Well, actually you can.  Even without using explicit →gotos.

I'm not sure if that's the most elegant solution, but the first thing
that comes to mind is that you can use dyadic predicate for stopping
condition as right operand rather than a number.

In this form (f⍣g)Y iterates until ((f Y) g Y) becomes true; for
example f⍣≡ is a fixed point of f.


      {¯1↓3↓({⍵,+/ ¯2 ↑ ⍵ } ⍣ {(↑⍵)<¯1↑⍺} ) ⍵, 0 1} 1000
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

(I passed the requested limit as (↑⍵) because GNU APL has no lexical
scope in dfns that would allow to name it without spilling it outside.)

-k



reply via email to

[Prev in Thread] Current Thread [Next in Thread]