Conditional function in APL

Is there a symbol or a known idiom of conditional function in any of the APL dialects?

I am sure that something is missing for me, because it is such a basic language element. In other languages, it is called a conditional statement, but I will avoid this term here because the APL statement is something completely different.

For example, do C and friends have x ? T : F x ? T : F
LISP have (if x TF)
Python has T if x else F
etc.

I know that modern APLs have :If and friends, but they are required to control the program flow: they do not return a value, cannot be used inside an expression, and, of course, cannot be applied to arrays of boolean values. They have a completely different goal, and this is very good.

The only decent expression that I could do for a functional choice is (FT)[⎕IO+x] , which doesn’t look particularly shortened or readable for me, although it does its job even on arrays:

  ('no' 'yes')[⎕IO+(⍳5)∘.>(⍳5)] no no no no no yes no no no no yes yes no no no yes yes yes no no yes yes yes yes no 

I tried to come up with a similar expression using the command, but failed unsuccessfully in boolean arrays. Even if I could, he would still have to insert ⎕IO or hardcoded 1, which is even worse in terms of readability.

Before moving on to defining my own if and using it in every program I will ever write, is there any canon? Am I missing an obvious function or operator?

(Are there any APL programmers on SO ?:-)

+7
source share
6 answers

Yes, there are APL programmers on SO (but not so much!).

I think the answer is that there is no standard on this.

For a scalar solution, I use "pick":

  x⊃ft 

While for a boolean array, I use indexing, as you do above:

 ft[x] 

I always use a zero index of the source, so there is no need to add 1, and pairs are not needed.

If they are not simple enough, I think you should cover them with an "if" function. It will also allow you to establish true and false in the most natural order t f.

+8
source

The problem with these:

 (ft)[x] xft xft 

consists in evaluating both t and f .

If you want to short-circuit an item, you can use protective devices:

 {x:t ⋄ f} 

It is equivalent

 if (x) { return t; } f; 

in a C-like language.

+8
source

An old, old idiom that did something like a C-ternary operator ? : ? : and returned the result, there was the following:

  r←⍎(¯3 3)[x=42]↑'6×8 ⋄ 6×7' 

Note that this is written to start 0, and the parens around -3 -3 for clarity.

x = 42 is evaluated as zero or one, depending on this answer we choose -3 or 3, and thus select and execute either the first 3 elements ("6x8") or the last 3 elements ("6x7") lines, Diamond ⋄ just for decoration.

Needless to say, there probably wouldn’t be code this way if you had: if: else avaiable, although the form of the control structure did not return the result.

+2
source

In Dyalog APL you can use:

 'value if true' (⊣⍣condition) 'value if false' 

The idea is to use (left tack - which always returns its left argument, discarding the correct argument) either 0 (for false) or 1 (for true) times - to the correct argument. Thus, if it is applied 0 times (i.e., not at all), the correct argument is returned unmodified, but if it is applied (once), then the left argument is applied. For example:.

  ab←3 5 Conditional←⊣⍣(a=b) 'match' Conditional 'different' different ab←4 4 Conditional←⊣⍣(a=b) 'match' Conditional 'different' match 

or

  Cond←{⍺(⊣⍣⍺⍺)⍵} bool←a=b 'match'(bool Cond)'different' match 
+2
source

This is a general question, I think the reason why there is no standard answer to it is because for the things you do with APL, it is actually less necessary than other languages.

However, this is sometimes necessary, and the way I implement the IFELSE in the GNU APL uses this function:

 ∇Z ← arg (truefn IFELSE falsefn) condition ;v v←⍬ →(0=⎕NC 'arg')/noarg v←arg noarg: →condition/istrue Z←falsefn v →end istrue: Z←truefn v end: 

A function can be called as follows:

  3 {'expression is true' ⍵} IFELSE {'was false' ⍵} 0 was false 3 

This particular implementation goes into the left argument as to the sentence, because sometimes it can be convenient. Without a left argument, it passes to .

0
source

APL expression:

 (1+x=0)⌷yz 

must be C language equivalent for

 x?y:z 

And all the rest

 (1+x>0)⌷yz 

for

 x<=0?y:z 

Etc. In the general case, if bc are expressions of the corresponding languages, the APL expression:

 (1+~a)⌷bc 

It should be equivalent to C language:

 a?b:c 
0
source

All Articles