Essays/Do While

From J Wiki
Jump to navigation Jump to search

f^:proposition^:_ y is a useful construct, meaning z=. y while. proposition z do. z=. f z end. . For example:

f=: 1 + 3 * ]
proposition=: 1e5&>

foo=: 3 : 0
 z=. y
 while. proposition z do. z=. f z end.
)

   f^:proposition^:_ ]3
206671
   foo 3
206671

   f^:proposition^:_ ]3e5
300000
   foo 3e5
300000

An advantage of the tacit version is that it makes it easier to examine the inner workings of the iteration. Thus:

   f^:proposition^:a: 3
3 10 31 94 283 850 2551 7654 22963 68890 206671



Contributed by Roger Hui.