Branching - deprecated?

General APL language issues
Post Reply
User avatar
jmosk
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Branching - deprecated?

Post by jmosk »

Has branching been removed in version 18?

Code: Select all

 test←{
     ⎕←1
     →Here
     ⎕←2
     Here:
     ⎕←3
 }
Reports 'Here' is not defined.

Also noted that

Code: Select all

    →0
being used to exit the function immediately, results in a syntax error.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
petermsiegel
Posts: 159
Joined: Thu Nov 11, 2010 11:04 pm

Re: Branching - deprecated?

Post by petermsiegel »

Tradfns (traditional functions) support branching and labels today, just as in the before times, though discouraged by many or even shunned by some. Dfns have never supported them, using recursion (and guards) instead. See the Dyalog Programming Reference Guide (Ch. 2 Defined Functions & Operators, p. 116) for the restrictions, and elsewhere for the syntax of dfns/ops.
Cheers.
User avatar
jmosk
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Branching - deprecated?

Post by jmosk »

So if it is still supported, why does my example of →0 produce a SYNTAX error?

If I write a function where I wish to abort under some condition, it seems I can't use a →0 to exit.

Code: Select all

    :If number > 100
       ⎕←'Too high, aborting'       
       →0
    :endif

So how do you perform this early exit without the →0.

How would you make a case statement where you wanted to jump to different labels based on the value?

Code: Select all

      →(Case1, Case2, Case3, Case4)[i]
      Case1: S1
        →Next
      Case2: S2
        →Next
      Case3: S3
        →Next
      Case4: S4
      Next:
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
User avatar
Morten|Dyalog
Posts: 460
Joined: Tue Sep 09, 2008 3:52 pm

Re: Branching - deprecated?

Post by Morten|Dyalog »

Both of those examples work for me, I think there must be something else in your function causing a SYNTAX ERROR.

An alternative to your branch-based select statement would be:

Code: Select all

:Select i
:Case 1
   S1
:Case 2
   S2
:Else
   ⎕←'Bad i'
:EndSelect
User avatar
jmosk
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Branching - deprecated?

Post by jmosk »

Morten reported the code worked for him. So I tried again.

Version: 18.0.38756.0 64 Unicode
Created: Jul 15 2020 at 19:56:53
Build ID: a3a21494

)ed test

Code: Select all

 test←{
     ⎕←1
     →Here
     ⎕←2
     Here:
     ⎕←3
 }
VALUE ERROR: Undefined name: Here
test[2] →Here
∧ - is under the H in Here

)ed test

Code: Select all

 test←{
     ⎕←1
     →0
     ⎕←2 
 }
SYNTAX ERROR
test[2] →0
∧ is under the →

.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
User avatar
Morten|Dyalog
Posts: 460
Joined: Tue Sep 09, 2008 3:52 pm

Re: Branching - deprecated?

Post by Morten|Dyalog »

OK, as Peter explained in one of the first posts in this thread: control structures and branching are only supported in TradFns. dfns are intended for making functional definitions using guards and recursion. No "moving parts".
User avatar
jmosk
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Branching - deprecated?

Post by jmosk »

Ah - yes - this does in fact work while test←{ } does not allow →

Code: Select all

 r←test
     ⎕←1
     →Here
     ⎕←2
     Here:
     ⎕←3
test
1
3
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
User avatar
kai
Posts: 141
Joined: Thu Jun 18, 2009 5:10 pm
Location: Hillesheim / Germany

Re: Branching - deprecated?

Post by kai »

Please read the documentation on traditional functions and dfns. They are very different.

A couple of hints:

* dfns have lexical scope, tfns have not
* all variables in dfns are local by definition, in tfns they are global by default.
* dfns don't have control structures, tfns have
* tfns may have labels but dfns not
* tfns support jumping to labels/lines, dfns not
Post Reply