LABTITLE=: 'Monad/Dyad'

NB. =========================================================
Lab Section INTRODUCTION

If u and v are functions, then f=: u : v defines f to be a function whose monadic case is u and whose dyadic case is v.

For example, if v=: +, then 3 v 4 is 7 and v 4 is 4, i.e. since the monadic case of + is the conjugate, which produces an effect only on a complex argument such as 6j10.

We may, however, define a function add that performs addition on two arguments, and increments a single argument. Thus:
)
add=: >: : +

3 add 4

add 4

add 4 5 6 7 8

v=: +

3 v 4

v 4

NB. =========================================================
Lab Section

Mnemonic names may be assigned to functions for the absolute value (or magnitude) and remainder (or residue) as shown below. However, the fact that these functions may each be used either monadically or dyadically can lead to some confusion. Thus:
)
mag=: |
res=: |

y=: 3 _4 5 _6

mag y

4 res y

4 mag y

res y

NB. =========================================================
Lab Section THE CAP FUNCTION

The cap function (denoted by [:) has empty domains, and therefore yields errors for any arguments. It may be used to define functions such as mag and res which yield errors unless used with the intended valence.

Illustrate this by experimenting  with the following functions:
)
mag=: | : [:

res=: [: : |

NB. =========================================================
Lab Section

The cap function may also be used in a train to cause a verb that follows it to be applied monadically.

For example, a vector may be scaled to unit length by dividing it by its length, the length being the square root (%:) of the sum (+/) of the squares (*:) of its elements. Thus:
)
scale=: ] % [: %: [: +/ *:

scale y

%: +/ *: scale y
