JPhrases/CaseStatements
15A. Case Statements
If ag generates indices for a gerund ger , we will call ag an agenda function, and the function ger@.ag a case statement. For example:
ger=: f`g`h ag=: #@$ case=: ger@.ag f=: -: g=: +: h=: *: (case 3);(case 2 3 4 5);(case i.3 4) +--------------------------+ |1.5|4 6 8 10| 0 1 4 9| | | |16 25 36 49| | | |64 81 100 121| +--------------------------+ f=: *: (case 3);(case 2 3 4 5);(case i.3 4) +------------------------+ |9|4 6 8 10| 0 1 4 9| | | |16 25 36 49| | | |64 81 100 121| +------------------------+
Certain agendas prove to be useful with a variety of gerunds: for example, the rank used above, as well as various classifications such as negative, zero, and positive; integral or fractional; real or complex; numeric or character; boxed or open; and the depth of boxing. Since indices may be negative, the result of an agenda may be negative; thus the case f`g`h@.* applies f if the argument is zero, g if it is positive, and h if it is negative.
Since an agenda such as the hook =<. (which tests for fractional or integral) might invoke a domain error (when the argument is character or boxed), it is often useful to extend an agenda to produce a result in such a case. If this result is _1, the corresponding function in the gerund (perhaps h=: 'ERROR'"_) may be simply appended to the normal cases. Thus, the test for integral may be defined (using adverse) as (=<.) :: _1: For example:
F=: (=<.) :: _1: F"0 x=: 0.5 _2 2 0 1 1 F 'abcd' _1
m0=: ~. ,. #/.~ Nub and count m1=: ({.,#)/.~ Nub and count a2=: et=: :: _1: Error in tail position m3=: I=: (-:<.)et Integral m4=: C=: -.@(-:+) et Complex a5=: ep=: :: _2: Error in penultimate position m6=: S=: *ep Signum test with error in penultimate m7=: B=: -.@(-:>) :: 0: Boxed m8=: R=: #@$ Rank
An “or” over an agenda (that is, applying a given function for any one of several cases distinguished by the agenda) can be achieved by placing the same function in several places in the gerund. Moreover, agendas may be used in combination, as illustrated below:
c=: co`cb@.B NB. Executes co if open; cb if boxed co=: -@|`+@.C NB. Minus magnitude if real; conjugate cb=: ]`(|.&.>)`(|:&.>)@.(R@>) NB. Reverse if opened is list; transpose c <i. 2 4 NB. if opened is a table cb <i.2 4 NB. The case chosen by the agenda B +---+ |0 4| |1 5| |2 6| |3 7| +---+ c 3j4 5 6j7 NB. (Open) complex argument 3j_4 5 6j_7 NB. Conjugate co 3j4 5 6j7 NB. The case chosen by the agenda B 3j_4 5 6j_7
The complete definition of c can be seen by fixing it:
c f. -@|`+@.(-.@(-: +) ::(_1:))`(]`(|.&.>)`(|:&.>)@.(#@$@>))@.(-.@(-: >) ::0:)