Dfns and assignment
Dfns
A dfn (pronounced "deefun" with a very short "u" sound) is a way of writing functions in APL. It starts and ends with curly braces {}
, has a right argument ⍵
(omega) and an optional left argument ⍺
(alpha).
3{⍺}5 ⍝ ⍺ is the (optional) left argument
3
{⍵}'apl' ⍝ ⍵ is the right argument
apl
{⍺}5 ⍝ Calling a dyadic function monadically results in an error
VALUE ERROR
{⍺}5
∧
3{⍵} ⍝ Calling a function without a right argument results in an error
SYNTAX ERROR: Missing right argument
3{⍵}
∧
Function valence
Most functions are either monadic or dyadic. Monadic functions take a single argument to their right and dyadic functions take two arguments, one to the right and one to the left. For example, plus is a dyadic function e.g. 3+4 5 6
and iota is a monadic function e.g. ⍳3
.
From here, when functions are first introduced, f⍵
("eff omega") denotes a monadic function f
and ⍺f⍵
("alpha eff omega") denotes a dyadic function.
Assignment
Names are assigned with the left arrow name ← expression
. We say "name gets [function or array]".
one←1
three←3
equals←=
plus←+
four←4
four equals one plus three ⍝ 1 means true, 0 means false
1
We can use a name in the same line in which it is defined. In production code it is best to avoid this unless an expression is very short.
Read the following as "squared numbers divided by the sum of squares":
squared÷+/squared←¯1 0 1 2*2
0.1666666667 0 0.1666666667 0.6666666667`
Problem Set 2

Utility Functions

Without using the tally
≢⍵
or shape⍴⍵
functions, create a function namedTally
which returns the number of elements in a vector.Tally 1 2 4 523 1 2 454 7

Create a function named
Mean
which returns the mean average of a numeric vector.Mean 1 2 4 523 1 2 454 141

Using
⌊⍵
("floor" i.e. round down), create a functionIsDivisibleBy
which returns1
if⍺
is divisible by⍵
and0
otherwise.15 IsDivisibleBy 5 1 15 IsDivisibleBy 6 0 12 IsDivisibleBy ⍳12 1 1 1 1 0 1 0 0 0 0 0 1
Answers
There are many ways to write functions which have the same effect, but here are some examples:
Tally←{+/⍵=⍵}
Mean←{(+/⍵)÷Tally ⍵}
IsDivisibleBy←{d=⌊d←⍺÷⍵}


What Remains

⍺ Mod ⍵
is a dyadic function which returns the remainder after its right argument⍵
number is divided by the left argument⍺
.16÷5 3.2 5 Mod 16 ⍝ 3 5s and 1 left over 1 0.2×5 ⍝ 0.2 5s left over 1 12 Mod 30 6 13 Mod 56 4 5 Mod 30 ⍝ 5 goes into 25 exactly 6 times 0
Write the
Mod
function as a dfn without using the magnitude/residue (
) glyph. 
Write a function
Split ⍵
which takes a fractional number and returns two numbers: its integer and fractional parts.Split 0 0 0 Split ¯10 ¯10 0 Split 7.62 7 0.62
Answers
Using only what has been introduced so far, here are some example solutions:
Mod←{⍵⍺×⌊⍵÷⍺}
Split←{floor,⍵floor←⌊⍵}


What Was In That Vector Again?
You should have a variable named
⎕AVU
in your workspace, from problem set 1. How many even numbers are there in
⎕AVU
?  What percentage of numbers in
⎕AVU
are odd numbers?  What percentage of numbers in
⎕AVU
are strictly negative?  What percentage of numbers in
⎕AVU
are strictly positive?  What do you notice about the percentage of strictly positive and negative numbers?
Answers
 \(132\)
 \(48.4375\)
 \(0\)
 \(99.609375\)
 I know vague questions like this are kind of rubbish, but did you notice that the percentages of strictly positive and negative numbers don't add up to 100%? This means that there must be zeroes in
⎕AVU
 but you already knew that from×/⎕AVU
, didn't you?
 How many even numbers are there in

Prime Time
A prime number is divisible only by itself and
1
.Write a dfn which returns
1
if its argument is prime and0
otherwise.IsPrime 21 0 IsPrime 17 1
Answer
Once again, there are many ways to write this function. Here is one example using only constructs which have already been introduced:
IsPrime←{2=+/div=⌊div←⍵÷⍳⍵}