J Labs

Monad/Dyad

(1 of 4) 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:


In [ ]:
add=: >: : +

In [ ]:
3 add 4

In [ ]:
add 4

In [ ]:
add 4 5 6 7 8

In [ ]:
v=: +

In [ ]:
3 v 4

In [ ]:
v 4

(2 of 4) INTRODUCTION (ctd)

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:


In [ ]:
mag=: |

In [ ]:
res=: |

In [ ]:
y=: 3 _4 5 _6

In [ ]:
mag y

In [ ]:
4 res y

In [ ]:
4 mag y

In [ ]:
res y

(3 of 4) 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:


In [ ]:
mag=: | : [:

In [ ]:
res=: [: : |

(4 of 4) THE CAP FUNCTION (ctd)

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:


In [ ]:
scale=: ] % [: %: [: +/ *:

In [ ]:
scale y

In [ ]:
%: +/ *: scale y

End of Lab


In [ ]: