cancel
Showing results for
Did you mean:

## rotate function differences between k and q

New Contributor II

Following is the implementation of rotate function

Orig:

k){\$[0h>@y;'`rank;98h<@y;'`type;#y;,/|(0;mod[x;#y])_y;y]}

Working by making slight changes in q

{\$[0h>(@)y;'`rank;\$[ 98h<(@)y;'`type; [ (#)y;(,/)(|)(0;mod[x;(#)y])_y;y ]  ] ] }

How is the strikethrough stuff working in k ?

4 REPLIES 4
New Contributor II

Your "q version" is actually a hybrid between k and q. A more q-native interpretation would look like:

`rotate2:{\$[0h>type y;'`rank;98h<type y;'`type;count y;raze reverse(0;x mod count y)_y;y]}`

The whole function is a conditional with multiple branches. The part you crossed out is just a check if the list is empty, in which case we don't bother with calculating the rotated version, we just return the original empty list. You seem to have broken up the conditional in your "q" version even though it is still possible to use the multi-branch version.

Valued Contributor

Watch out The q language is implemented as a DSL embedded in the k4 programming language. The latter is exposed infrastructure. It is neither supported nor documented, and its use is deprecated – and in production code, strongly deprecated.

New Contributor II

Thanks @SJT and @gyorokpeter-kx

``````l  / 2 3 5 7 11j
2 rotate l / 5 7 11 2 3j
{\$[0h>(@)y;'`rank; 98h<(@)y;'`type; (#)y ;  (,/)(|)(0;mod[x;(#)y])_y  ; y  ] } [2;l]  /5 7 11 2 3j``````

I guess the above works

Valued Contributor

Huh – now I see what you’re up to: implementing `rotate` in q.

This reminds me of a category of puzzle that used to be popular in q’s ancestor language APL, called “dead key” questions. The premise was always that a certain key on your keyboard was not working; how do you code around it?

APL primitives are mostly variadic: they can be applied as either unaries or binaries, usually with related semantics. And they are (almost) all single characters, which is what gives the “dead key” problem its bite. For example, `rotate` and `reverse` in q are in APL the binary and unary forms of `⊖`

``````      l←2 3 5 7 11
⊖l
11 7 5 3 2
2⊖l
5 7 11 2 3``````

In k, operators are similarly variadic; q replaces the unary forms with keywords. Like k itself, the unary forms of the operators are considered exposed infrastructure: use the q keywords instead. The `(#)` you asked about is, as Péter advises, in q written as `count`