Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a niladic function

Tags:

apl

A non-niladic function F can be assigned to a variable G with

G ← F

However, if F is niladic, how do I prevent it from being evaluated?

like image 426
August Karlstrom Avatar asked Jan 24 '19 13:01

August Karlstrom


2 Answers

You can't.

In a way, niladic functions behave like arrays, except their value isn't determined until they are used. This also means that they exhibit value semantics rather than reference semantics. Note also, that niladic functions cannot be operands of operators, but rather their result will become the operand.

A way to circumvent both of these issues, is to wrap the niladic function in a dfn so that it takes a dummy argument (or two), and thus:

G←{F}
like image 181
Adám Avatar answered Oct 01 '22 05:10

Adám


APL evaluates expressions from right to left. As soon as all arguments of a function are present the function is evaluated and the function and its arguments are replaced by the result of the evaluation.

Now consider your example G←F. If F is monadic or duadic, then F cannot be evaluated because its right argument is missing. In parser terminology the token F is shifted rather than being reduced, The first expression that can be reduced is then G←F which assigns F to G.

On the other hand, if F is niladic, then F can be (and therefore will be) evaluated immediately (with, say, result Z), so that the assignmet will be G←Z and not G←F.

like image 30
Jürgen Sauermann Avatar answered Oct 01 '22 05:10

Jürgen Sauermann