Conditional Execution


A perennial question - if a condition is true then a function needs to be run, but if the condition is false then it doesn't.  Obviously there are further permutations and elaborations - but the answer(s) probably take similar forms to that for the simple case.

The Traditional Approach

      cond←1
      ⎕vr 'foo'
     ∇ z←a foo w                   
[1]    z←a+w                       
     ∇                             
      2 foo 3
5
      ⍎cond/'2 foo 3'
5
      cond←0
       ⍎cond/'2 foo 3'

In its favour:
Against:

Control Structures

      cond←1
      ⎕vr 'foo1'
     ∇ z←a foo1 w                                                                                     
[1]    :If cond                                                                                       
[2]        z←a foo w                                                                                  
[3]    :Else                                                                                          
[4]        z←⍬                                                                                        
[5]    :EndIf                                                                                         
     ∇                                                                                                
      2 foo1 3
5
      cond←0
      2 foo1 3

In its favour:
Against:

Define an Operator

      cond←1
      ⎕vr 'ofoo'
     ∇ z←a(cond ofoo foo)w                                                                                     
[1]    :If cond                                                                                                
[2]        z←a foo w                                                                                           
[3]    :Else                                                                                                   
[4]        z←⍬                                                                                                 
[5]    :EndIf                                                                                                  
     ∇                                                                                                         
       2(cond ofoo foo)3
5
      cond←0
        2(cond ofoo foo)3

In its favour:
Against:

The Power Operator     

      cond←1
      2 (foo⍣cond) 3
5
      cond←0
       2 (foo⍣cond) 3
3

In its favour:
Against:


Page last updated 2 December 2008.
Copyright Dogon Research 2008.