Options For Conditional Statements

For users of dfns, both novice and expert
Post Reply
conor_f
Posts: 13
Joined: Wed May 29, 2013 7:33 pm

Options For Conditional Statements

Post by conor_f »

What options can be used for conditional statements while using D-Fns? I am aware of using the colon in conjunction with the diamond separator, but that is very limited as program flow does not return. I.e each side of the diamond separator are evaluated as the end of the function. As far as I'm aware, you can't use the :IF :ENDIF etc constructs within D-Fns or the branch operator either. Could someone enlighten me as to the different possibilities?

Thanks in advance
JohnS|Dyalog

Re: Options For Conditional Statements

Post by JohnS|Dyalog »

Hi Conor: you're right, dfns have only one control element: the guard. IMHO trad(itional)fns are best for procedural programming or "scripting" (do this, then do that, ...), while dfns are good for a more declarative style, where the result is just an expression (preceded by zero or more local definitions used within the expression).

"Program flow does not return [after a guard/diamond]". That's correct but you could use an inner dfn to define which alternative the resulting expression needs:

      {
thing ← {cond:this ⋄ that}⍵ ⍝ if cond, then thing is this, else thing is that
expr ... thing ... ⍝ result is ... thing ...
}

It takes a while to get the hang of this style: see http://dfns.dyalog.com/n_contents.htm for some examples.
User avatar
Phil Last
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Options For Conditional Statements

Post by Phil Last »

Also there is the power operator (⍣) which, given an integer scalar right operand (n) applies its left function operand (f) to its right or between its left and right arguments n times. In particular if n is boolean f is applied once or not at all. As it derives a function it can be used in any context.

for boolean returning function c:
      :If c r ⋄ r←f r ⋄ :End    ⍝ traditional

r←{c r:f r ⋄ r}... ⍝ direct

r←f⍣(c r)⊢r ⍝ tacit
JohnS|Dyalog

Re: Options For Conditional Statements

Post by JohnS|Dyalog »

See also Phil's conditional operator: http://dfns.dyalog.com/n_cond.htm
conor_f
Posts: 13
Joined: Wed May 29, 2013 7:33 pm

Re: Options For Conditional Statements

Post by conor_f »

Thank you very much John and Phil, nesting D-Fns will work perfectly for me in this case.

I am finding dfns.dyalog.com a very useful resource while learning APL :)
Post Reply