top | item 29350997

(no title)

johan_felisaz | 4 years ago

Genuine question, what would be the idiomatic way of doing an impure operation multiple times in J/APL ? (e.g. if writing an interpreter loop)

I was wondering if it's doable without the while. and other constructs (which honestly feel like plugged artificially in J, even syntax wise)

discuss

order

mlochbaum|4 years ago

The idiomatic way would be to exit the array paradigm and use an imperative (while.) or functional (recursion) method. APL and J both have "repeat until convergence" functionality, which stops when the same result is returned twice in a row, but to use this you'd have to artificially create a result that changes each time.

When designing BQN I embraced the limited nature of array primitives, so that most primitives can only implement efficiently parallelizable programs and none of them can perform infinite loops. Flip this around and you get guarantees: if you create a function by composing primitives you know it will halt, and if you avoid using modifiers in complicated ways it's easy to prove good sequential and parallel bounds on the runtime.[0] Although BQN has no tail recursion (J also doesn't; Dyalog does), it's possible to implement loop functionality that uses only logarithmic stack space in the number of iterations, with low overhead (I just measured 30ns/iteration in CBQN for a simple incrementing loop).[1]

[0] https://mlochbaum.github.io/BQN/doc/primitive.html

[1] https://mlochbaum.github.io/BQN/doc/control.html#low-stack-v...

RodgerTheGreat|4 years ago

in K, there is an adverb form which is essentially equivalent to a while loop: apply a function or composition to a value repeatedly as long as a second function or composition of that value yields true.

It's basically an "escape hatch" when an algorithm cannot be cast into any more specific pattern, like iteration, a fixed-point, a reduction, etc. In practice it is needed very rarely.

There's a complete list of adverb forms for k6 here: https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual....