Page 1 of 1

Branching - deprecated?

Posted: Thu Feb 15, 2024 6:26 pm
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.

Re: Branching - deprecated?

Posted: Fri Feb 16, 2024 5:09 am
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.

Re: Branching - deprecated?

Posted: Tue Feb 20, 2024 9:26 pm
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:

Re: Branching - deprecated?

Posted: Wed Feb 21, 2024 8:14 am
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

Re: Branching - deprecated?

Posted: Wed Feb 21, 2024 1:21 pm
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 →

.

Re: Branching - deprecated?

Posted: Wed Feb 21, 2024 4:53 pm
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".

Re: Branching - deprecated?

Posted: Wed Feb 21, 2024 5:08 pm
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

Re: Branching - deprecated?

Posted: Wed Feb 21, 2024 5:38 pm
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