Branch: Difference between revisions

Jump to navigation Jump to search
252 bytes added ,  22:18, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(#Abort)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
Branch was the only form of flow control in early versions of APL, having been carried over from the conditional flow arrows of [[Iverson Notation]]. Some APL dialects later added the more modern [[control structure]]s leading to deprecation of Branch.
Branch was the only form of flow control in early versions of APL, having been carried over from the conditional flow arrows of [[Iverson Notation]]. Some APL dialects later added the more modern [[control structure]]s leading to deprecation of Branch.


Branch is unusual having syntax resembling a [[monadic function]], but affecting program flow rather than altering arrays. <span id=Abort>In addition, a so called "naked branch" or"abort", that is, a [[niladic]] <source lang=apl inline>→</source> will cut one level off the call stack.</span>
Branch is unusual having syntax resembling a [[monadic function]], but affecting program flow rather than altering arrays. <span id=Abort>In addition, a so called "naked branch" or"abort", that is, a [[niladic]] <syntaxhighlight lang=apl inline>→</syntaxhighlight> will cut one level off the call stack.</span>


In a [[tradfn]], Branch can take a [[scalar]] or [[vector]] argument, and immediately cause execution to resume on the line indicated by the first element of the argument, which must be a non-negative integer. If the line number is 0, the function will return to its caller. If the argument is empty, the effect is that of consuming the argument and giving no result, equivalent to the dfn <source lang=apl inline>{}</source>. Inserting a label (an identifier followed by a colon <source lang=apl inline>:</source>) into a function, in a sense declares a name as a constant with the scalar value of the current number for whichever line the label occurs on. This ensures stability when lines are inserted into a function.
In a [[tradfn]], Branch can take a [[scalar]] or [[vector]] argument, and immediately cause execution to resume on the line indicated by the first element of the argument, which must be a non-negative integer. If the line number is 0, the function will return to its caller. If the argument is empty, the effect is that of consuming the argument and giving no result, equivalent to the dfn <syntaxhighlight lang=apl inline>{}</syntaxhighlight>. Inserting a label (an identifier followed by a colon <syntaxhighlight lang=apl inline>:</syntaxhighlight>) into a function, in a sense declares a name as a constant with the scalar value of the current number for whichever line the label occurs on. This ensures stability when lines are inserted into a function.


[[Dfn]]s do not support branching except the naked branch to cut the stack.
[[Dfn]]s do not support branching except the naked branch to cut the stack.
Line 15: Line 15:
==Examples==
==Examples==
Branch function can be used to implement [[wikipedia:Structured programming|structured programming]] constructs without the use of special keywords:<ref>[[Garth Foster|Garth H. Foster]]. (1975). [https://doi.org/10.1145/800117.803792 What lies beyond the branch arrow?].</ref>
Branch function can be used to implement [[wikipedia:Structured programming|structured programming]] constructs without the use of special keywords:<ref>[[Garth Foster|Garth H. Foster]]. (1975). [https://doi.org/10.1145/800117.803792 What lies beyond the branch arrow?].</ref>
<source lang=apl>
<syntaxhighlight lang=apl>
       ⍝ If-Then-Else construct
       ⍝ If-Then-Else construct
       →(~B)/Else
       →(~B)/Else
Line 23: Line 23:
         S2
         S2
       End:
       End:
</source>
</syntaxhighlight>
<source lang=apl>
<syntaxhighlight lang=apl>
       ⍝ Select construct
       ⍝ Select construct
       →(Case1, Case2, Case3, Case4)[i]
       →(Case1, Case2, Case3, Case4)[i]
Line 35: Line 35:
       Case4: S4
       Case4: S4
       Next:
       Next:
</source>
</syntaxhighlight>
<source lang=apl>
<syntaxhighlight lang=apl>
       ⍝ While-Do construct
       ⍝ While-Do construct
       While: →(~B)/Done
       While: →(~B)/Done
Line 42: Line 42:
         →While
         →While
       Done:
       Done:
</source>
</syntaxhighlight>
<source lang=apl>
<syntaxhighlight lang=apl>
       ⍝ Repeat-Until construct
       ⍝ Repeat-Until construct
       Repeat: S2
       Repeat: S2
         →(~B)/Repeat
         →(~B)/Repeat
</source>
</syntaxhighlight>
Note that <source inline lang=apl>Else</source>, <source inline lang=apl>End</source>, <source inline lang=apl>CaseN</source>, <source inline lang=apl>Next</source>, <source inline lang=apl>While</source>, <source inline lang=apl>Done</source>, <source inline lang=apl>Repeat</source> in the examples above are all user-defined labels rather than keywords.
Note that <syntaxhighlight inline lang=apl>Else</syntaxhighlight>, <syntaxhighlight inline lang=apl>End</syntaxhighlight>, <syntaxhighlight inline lang=apl>CaseN</syntaxhighlight>, <syntaxhighlight inline lang=apl>Next</syntaxhighlight>, <syntaxhighlight inline lang=apl>While</syntaxhighlight>, <syntaxhighlight inline lang=apl>Done</syntaxhighlight>, <syntaxhighlight inline lang=apl>Repeat</syntaxhighlight> in the examples above are all user-defined labels rather than keywords.


==External links==
==External links==

Navigation menu