8. Turning up the Heat
In this problem, you’re asked to enter your own APL function, without giving it any arguments. Your function will be checked by the APL Challenge system using several tests to see whether it is correct.
In problem 7, you saw how to write a function that takes a right argument using ⍵. Your function can also take a left argument by using ⍺ (alpha, the left-most letter of the Greek alphabet); any ⍺ is replaced by the data to the left of the opening curly bracket ( { ). For example, a function that sums its left and right arguments and then multiplies by two could be written {2×⍺+⍵}.
In problem 5, you saw how we can create lists simply by putting elements next to each other. This also works for argument names that are separated by spaces:
{⍵ ⍵ ⍵} 4
4 4 4
{⍵ ⍵ ⍵} 'd'
ddd
We call this Stranding, like a strand of thread.
Another way to join things is by using , (Comma). For example, 2 , 3 gives 2 3. This is a normal function, so it follows the rules you learnt in problem 4. Stranding and , behave differently when at least one element is a list. In such a case, , joins the two arguments into a single list:
'HOT' , 'DOG'
HOTDOG
In contrast, stranding of two things always creates a two-element list:
'HOT' 'DOG'
┌───┬───┐
│HOT│DOG│
└───┴───┘
Write a function using Stranding, ⍴ (from problem 4), and , that takes a letter as its left argument and a size as its right argument, then produces a square table of the given size. The table should be made entirely of plus symbols (+), replaced by the given letter marking the diagonal from the top left to the bottom right:
'O' {answer} 3
O++
+O+
++O
'Q' {answer} 4
Q+++
+Q++
++Q+
+++Q