Options For Conditional Statements

For users of dfns, both novice and expert

Options For Conditional Statements

Postby conor_f on Mon Jul 15, 2013 11:13 pm

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
conor_f
 
Posts: 13
Joined: Wed May 29, 2013 7:33 pm

Re: Options For Conditional Statements

Postby JohnS|Dyalog on Tue Jul 16, 2013 6:11 am

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
JohnS|Dyalog
 
Posts: 173
Joined: Wed Sep 10, 2008 10:01 am

Re: Options For Conditional Statements

Postby Phil Last on Tue Jul 16, 2013 7:19 am

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
User avatar
Phil Last
 
Posts: 464
Joined: Thu Jun 18, 2009 6:29 pm

Re: Options For Conditional Statements

Postby JohnS|Dyalog on Tue Jul 16, 2013 7:53 am

See also Phil's conditional operator: http://dfns.dyalog.com/n_cond.htm
User avatar
JohnS|Dyalog
 
Posts: 173
Joined: Wed Sep 10, 2008 10:01 am

Re: Options For Conditional Statements

Postby conor_f on Tue Jul 16, 2013 8:38 am

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 :)
conor_f
 
Posts: 13
Joined: Wed May 29, 2013 7:33 pm


Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest