rak1507s solution above returns radians, while you asked for degrees, so thats a good place to start.

Degrees are radians נ180/?, and pi is the arc-cosine of -1. So we could extend his solution:

q)ab:4
q)bc:5
q)atan ab%bc
0.6747409
q)atan[ab%bc]*180%acos -1
38.65981

If this were to solved for different triangles, we would want to define a function . How would we write it?

It is a function of two quantities, and its math. Default arguments `x`

and `y`

will do fine.

wota:{atan[x%y]*180%acos -1} / work out the angle

Clearly 180/? is a constant we need not calculate repeatedly.

q)wota:{x*atan y%z}[180%acos -1;;] / work out the angle

q)wota . 4 5

38.65981

Above, the constant is calculated when `wota`

is defined, and its value bound to the definition. This is a useful technique with constants.

You might even prefer your function defined as a composition. Below, the three unaries `(180%acos -1)*`

, `atan`

, and `.[%]`

are composed into a function by suffixing them with the general null `::`

. Again, the constant is calculated only at definition time.

q)wota:(180%acos -1)* atan .[%] :: / work out the angle

q)wota 4 5

38.65981

Megan, you named this *My Programming Journey*, so some travel notes

Your prior experience has taught you to read in data to work on and, of course, with substantial data sets that is exactly right. But the q REPL offers you a new way to work. A good path is to experiment with q as a calculator on small data structures, and paste into your text editor what works for you. You can also usefully defer formatting output and focus on calculating the value you want.

Starting in the REPL also helps you explore algorithms more thoroughly than save-load-and-run. Setting aside rjak1507s trigonometrical insight, lets see what that would look like with your algorithm.

q)ab:4; bc:5
q)show mc:bm:.5* sqrt sum {x*x}(ab;bc) / lengths BM, MC
3.201562
q)a:bc; b:mc; c:bm / new triangle
q)show air: acos a%2*b / angle in radians
0.6747409
q)air*180%acos -1
38.65981

That could become a lambda:

wota0:{[r2d;ab;bc] /radians>degrees; AB; BC mc:bm:.5* sqrt sum {x*x}(ab;bc); / lengths BM, MC a:bc; b:mc; c:bm; / new triangle r2d * acos a%2*b }[180%acos -1;;]

From which we notice variable `c`

is set but not read, and that `mc`

and `bm`

are synonyms.

wota1:{[r2d;ab;bc] /radians>degrees; AB; BC bm:.5* sqrt sum {x*x}(ab;bc); / lengths BM, MC r2d * acos bc%2*bm }[180%acos -1;;]

Now we notice the definition of `bm`

finishes with `.5*`

but its doubled when read in the result expression. We end up with:

wota2:{[r2d;ab;bc]r2d*acos bc%sqrt sum{x*x}(ab;bc)}[180%acos -1;;]

If you were collecting trig functions you might be happier returning radians and defining conversion functions for re-use.

wota3:{acos x%sqrt(x*x)+y*y} /work out the angle r2d:(180%acos -1)* /radians=>degrees d2r:(acos[-1]%180)* /degrees=>radians

At the 40th anniversary celebration of the British APL Association at the Royal Society in 2004, Arthur Whitney spoke about k, the language in which q is implemented:

It is theoretically impossible for k to outperform C, because it compiles into C. For every k program there is an equivalent C program that runs exactly as fast.

Yet k programs routinely outperform hand-coded C. How is this possible? Its because it is a lot easier to find your errors in 4 lines of k than in 400 lines of C.

Treasure terseness.