Nested guards

For users of dfns, both novice and expert

Nested guards

Postby Phil Last on Fri Sep 22, 2017 11:29 am

We need four tests to conform that an argument is a list of strings.
    Is it an array rather than a space?
    Is it depth two?
    Is it rank one?
    Are all its items strings?
You could argue about the best order to ask them in but we need 'em all and we wouldn't necessarily want to ask the last one if it turned out that the argument was actually a 1G by 1k floating array - and it wouldn't give the right answer anyway - so
won't cut it.
      2∊⎕NC'⍵':2=≡⍵:1=≢⍴⍵:∧/(''≡0∘/)¨⍵ ⋄ 0
      2∊⎕NC'⍵':{2=≡⍵:{1=≢⍴⍵:∧/(''≡0∘/)¨⍵ ⋄ 0}⍵ ⋄ 0}⍵ ⋄ 0
Above presents three versions of the same bit of code. The first doesn't work. The second and third are just silly requiring two extra levels of embedded dfn just to allow nested guards. The third is arguably more readable than the second.

I'd like two things. First is to allow the "result expression" of a guard itself to be a guard. "AndIf" would be the obvious implication. It's modelable by use of the dop:
      and←{⍺⍺⊣⍵:⍵⍵⊣⍵ ⋄ 0}
requiring all operands to be braced or parenthesised:
If and were a primitive conjunction all well and good; as it is it only hides the issue.
APL# proved there are no counter examples of the nested guard's rectitude.

The other thing I'd like is a better and more concise way to test for lists of strings.

p.s. OK we don't need the first test. If it's a space its depth and rank are both zero. I just wanted an example that had four tests!
User avatar
Phil Last
Posts: 433
Joined: Thu Jun 18, 2009 6:29 pm

Re: Nested guards

Postby JohnS|Dyalog on Sat Sep 23, 2017 11:12 am

Hi Phil,
1. I've asked the language group how we feel about "guarded guards".
2. A quick test for a list-of-strings might be nice. This gets you some of the way:
      strings ← 0∧.= 10|⎕DR¨
User avatar
Posts: 160
Joined: Wed Sep 10, 2008 10:01 am

Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest