Why LLMs Can’t Write q/kdb+: Writing code Right-to-Left gabiteodoru 7 min read · 1 day ago 1 day ago -- Listen Share
In my previous post I showed how LLMs struggle with coding in q/kdb+; having immediate feedback loops using the q-MCP server does help a bit, but could we do better? Diving deep, a first problem becomes obvious: evaluation order! Claude incorrectly wrote 0.5*y+x%2*y for the Newton’s Method update. The expression would be correct, except for missing some parantheses. But which ones?
in Python, the correct expression would be 0.5*y+x/(2*y)
in q/kdb+ it’d be (0.5*y)+x%2*y
Why the difference? Because q/kdb+ as well as its ancestors APL, J, and k, feature Right-to-Left with No Operator Precedence (RL-NOP) evaluation. Claude is aware of that, but it struggled to write correct code based on those rules; in fact, it wrote something that’s half-Python, half-q, and correct in neither. In general LLMs struggle with RL-NOP.
However, RL-NOP is a fundamental design choice for APL and its descendants. Turing-award winner and creator of APL, Ken Iverson, wrote:
3. An expression evaluated from right to left is the easiest to read from left to right. For example, the expression a+x×b+x×c+x×d+x×e+x×f (for the efficient evaluation of a polynomial) is read as a plus the entire expression following, or as a plus x times the following expression, or as a plus x times b plus the following expression, and so on. 4. In the definition F/x ≡ x1 F x2 F x3 F … F x⍴x the right-to-left convention leads to a more useful definition for nonassociative functions F than does the left-to-right convention. For example, -/x denotes the alternating sum of the components of x , whereas in a left-to-right convention it would denote the first component minus the sum of the remaining components. Thus if d is the vector of decimal digits representing the number n , then the value of the expression 0=9|+/d determines the divisibility of n by 9 ; in the right-to-left convention, the similar expression 0=11|-/d determines divisibility by 11 .
Far from me to disagree with a Turing-award winner on which direction is easiest to read! But writing from right to left is simply something LLMs are bad at. And it’s not the same as translation to Arabic or Hebrew; direction here refers to the temporal order in which the tokens are produced; even for right-to-left languages, the order in which the tokens get produced remains unchanged; rather, a thin display layer handles the visual presentation. As a fun fact, this layer is missing when using Claude Code CLI, leading to:
> Translate "Algebra" to Hebrew
● הרבגלא
... continue reading