Mixing :OrIf and :Return

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

Mixing :OrIf and :Return

Postby ray on Tue Mar 31, 2015 11:19 am

Can one mix :Return with :OrIf?

EG:
∇ age1 foo age2
[1] :If (age1<age2)
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] :Return
[4] :OrIf (age1=age2)
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] :Return
[7] :OrIf (age1>age2)
[8] 'Age ',(⍕age1),' is greater than ',⍕age2
[9] :Return
[10] :End

Lets try:
49 50 51 foo¨ 50
Age 49 is greater than 50
Age 50 is less than 50
Age 51 is less than 50

This suggests that one should not mix the two.

(I always try to avoid using :OrIf as I find the logic too confusing.)
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 222
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: Mixing :OrIf and :Return

Postby DanB|Dyalog on Tue Mar 31, 2015 10:12 pm

You can mix the two but you must understand how :OrIf works.
If you have doubts try translating your code with 'good ol' branches':

Code: Select all
     ∇ age1 foo2 age2
[1]    →(age1<age2)/goforit                     ⍝ :If (age1<age2)
[2]    'Age ',(⍕age1),' is less than ',⍕age2
[3]    :Return
[4]    →(age1=age2)/goforit                     ⍝ :OrIf (age1=age2)
[5]    'Age ',(⍕age1),' is equal to ',⍕age2
[6]    :Return
[7]    →(age1>age2)↓0                           ⍝ :OrIf (age1>age2)
[8]   goforit:'Age ',(⍕age1),' is greater than ',⍕age2
     ∇

as you can see the meaning of all but the last Orif is to execute the code (the last OrIf skips if it is NOT true) and because you exit the program with :return lines [4-7] will never be executed.
What you probably want is this:

Code: Select all
     ∇age1 foo3 age2
[1]    →(age1<age2)↓eq
[2]    'Age ',(⍕age1),' is less than ',⍕age2
[3]    :Return
[4]   eq:→(age1=age2)↓gt
[5]    'Age ',(⍕age1),' is equal to ',⍕age2
[6]    :Return
[7]   gt:→(age1>age2)↓0          ⍝ unnecessary
[8]   'Age ',(⍕age1),' is greater than ',⍕age2
     ∇

or, in control structure terms:

Code: Select all
     ∇ age1 foo4 age2
[1]    :If (age1<age2)
[2]        'Age ',(⍕age1),' is less than ',⍕age2
[3]   ⍝ :Return - no need
[4]    :ElseIf (age1=age2)
[5]        'Age ',(⍕age1),' is equal to ',⍕age2
[6]   ⍝ :Return - no need
[7]    :Else ⍝ (age1>age2)
[8]        'Age ',(⍕age1),' is greater than ',⍕age2
[9]    :EndIf
     ∇

I think :OrIfs are useful but not if you put too much code between them. Then it becomes difficult to read and can be confusing, specially when the code to execute is 25 lines down with lots of OrIfs in between.
DanB|Dyalog
 


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest