<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Faizan Kazmi</title>
    <description>The latest articles on DEV Community by Ali Faizan Kazmi (@alifaizankazmi).</description>
    <link>https://dev.to/alifaizankazmi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F117351%2F4cc20ed1-a116-4357-892c-cc6ce7e8f82b.jpg</url>
      <title>DEV Community: Ali Faizan Kazmi</title>
      <link>https://dev.to/alifaizankazmi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alifaizankazmi"/>
    <language>en</language>
    <item>
      <title>Koping with KDB 002: More List Reversal</title>
      <dc:creator>Ali Faizan Kazmi</dc:creator>
      <pubDate>Sat, 29 Dec 2018 19:00:36 +0000</pubDate>
      <link>https://dev.to/alifaizankazmi/koping-with-kdb-002-more-list-reversal-3kd5</link>
      <guid>https://dev.to/alifaizankazmi/koping-with-kdb-002-more-list-reversal-3kd5</guid>
      <description>&lt;h5&gt;
  
  
  Estimated reading time: 10 minutes
&lt;/h5&gt;

&lt;p&gt;In the previous &lt;a href="https://dev.to/alifaizankazmi/koping-with-kdb-001-list-reversal-3079"&gt;post&lt;/a&gt; we stopped short of applying recursion to reverse a list since we found a quirkier way to do it. Let's see if the use of recursion results in simpler code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give me your &lt;code&gt;ifs&lt;/code&gt;, your &lt;code&gt;elses&lt;/code&gt;...
&lt;/h2&gt;

&lt;p&gt;Yes, it is possible to control a Q script's execution using conditional evaluation, although it is discouraged: restructuring your code to avoid conditional evaluation is preferred whenever possible.&lt;/p&gt;

&lt;p&gt;Available to us is the &lt;code&gt;if&lt;/code&gt; statement:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n: 1
if[n=1;message: "I don't think this is useful to us"]

message
-&amp;gt; "I don't think this is useful to us"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The if statement evaluates a given condition (&lt;code&gt;n=1&lt;/code&gt;) and if the condition evaluates to a value greater than 0 (which represents the boolean &lt;code&gt;false&lt;/code&gt;) then any statements following the condition are executed in order from left to right. In our case, we are assiging the string &lt;code&gt;"I don't think this is useful to us"&lt;/code&gt; to the variable &lt;code&gt;message&lt;/code&gt;. &lt;/p&gt;




&lt;p&gt;As an aside, notice that the concept of scope doesn't apply here: the variable &lt;code&gt;message&lt;/code&gt; is defined inside the &lt;code&gt;if&lt;/code&gt; statement but continues to be accessible outside of it!&lt;/p&gt;




&lt;p&gt;Yes, this doesn't fit our use case: if we are to use recursion, we also need an &lt;code&gt;else&lt;/code&gt; clause - something like the following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if base case reached
    return
else
    continue list reversal
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  ...or just give me your ternaries
&lt;/h2&gt;

&lt;p&gt;Q supports an equivalent of the ternary operator &lt;code&gt;?&lt;/code&gt; available in languages such as Java, JavaScript and C#:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$[1b;"Execute if true";"Execute if false"]
-&amp;gt; "Execute if true"

$[0b;"Execute if true";"Execute if false"]
-&amp;gt; "Execute if false"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first expression given to the &lt;code&gt;$&lt;/code&gt; operator (&lt;code&gt;1b&lt;/code&gt; or &lt;code&gt;0b&lt;/code&gt;) is the condition to evaluate. If the condition evaluates to true then the second expression is executed, otherwise the third one is. If you want to execute multiple expressions for one condition then you can encapsulate your expressions in square brackets:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$[1b;[a:2;b:3];c:4]
a
-&amp;gt; 2
b
-&amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I think we know enough to proceed with reversing our list. Onward to recursion!&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple(r) example
&lt;/h2&gt;

&lt;p&gt;Before we apply recursion to our problem, we can try testing it on a simpler problem to make sure it works as expected. We can define a recursive function which keeps adding 1 to a given number until the number is equal to 6:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;recur: {[number] $[number=6;"Done";recur[1 + number]]}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looks like it works!&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;recur 1
-&amp;gt; "Done"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Can we be sure that recursion is taking place, though? Since I'm not aware of the equivalent of &lt;code&gt;println&lt;/code&gt; in Q, one way to make sure that recursion is in fact happening is to pass a number greater than 6 to the function. In languages like Java this should generate a stack overflow since the recursive function would keep on calling itself until there was no more space on the stack. That's exactly what happens here, too:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;recur 7
-&amp;gt; `stack
-&amp;gt; @
-&amp;gt; {[number] $[number=6;"Done";recur[1 + number]]}
-&amp;gt; 2008
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;No idea what the &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;2008&lt;/code&gt; mean. I'll probably come back to how Q displays exceptions later (Q exceptions prefer to be addressed as "signals").&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, recursion
&lt;/h2&gt;

&lt;p&gt;No, wait. There's one more operation we need before we can construct our function: the &lt;code&gt;join&lt;/code&gt; operator, used to join two lists. The operator is simply a comma:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 3,4 5 6
-&amp;gt; 1 2 3 4 5 6

1,2 3 4
-&amp;gt; 1 2 3 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Sorry, one more thing: we also need a way to reduce the size of the list with each recursive call until we're left with a list of one element. Enter the &lt;code&gt;cut&lt;/code&gt; (&lt;code&gt;_&lt;/code&gt;) operator:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list: 1 2 3

1 _ list
-&amp;gt; 2 3

-1 _ list
-&amp;gt; 1 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At long last, here's our function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list] 
    $[1 = count list;
        list;
        reverseList[1 _ list],-1 _ list
    ]
}

reverseList 1
-&amp;gt; 1
reverseList 1 2
-&amp;gt; 2 1
reverseList 1 2 3
-&amp;gt; 3 2 1 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Uh-oh, the last result is clearly wrong. Why? Let's do a dry run:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList 1 2 3 -&amp;gt; call reverseList[2 3]
reverseList 2 3 -&amp;gt; call reverseList 3
reverseList 3 -&amp;gt; return 3
reverseList 2 3 -&amp;gt; return 3 joined with 2
reverseList 1 2 3 -&amp;gt; return 3 2 joined with 1 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Therein lies the fault: it would've been more obvious if I had formulated a proper algorithm:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given a list L
Its reverse is a reverse of the list t(L) 
    (where t denotes the tail - e.g., t(1 2 3) is 2 3) 
Joined with h(L) 
    (where h denotes the head - e.g., h(1 2 3) is 1)
The reverse of a list with one element is the list itself
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In short, the last expression we passed to the &lt;code&gt;$&lt;/code&gt; operator is wrong. We should've used the &lt;code&gt;take&lt;/code&gt; (&lt;code&gt;#&lt;/code&gt;) operator:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list: 1 2 3
1#list
-&amp;gt; ,1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The comma before &lt;code&gt;1&lt;/code&gt; is just a way of indicating that &lt;code&gt;1&lt;/code&gt; is not a number: it is a single-item list.&lt;/p&gt;

&lt;p&gt;Here goes nothing:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list] 
    $[1 = count list;
        list;
        reverseList[1 _ list],1#list
    ]
}

reverseList 1
-&amp;gt; 1
reverseList 1 2
-&amp;gt; 2 1
reverseList 1 2 3
-&amp;gt; 3 2 1
reverseList "A string is a list of characters"
-&amp;gt; "sretcarahc fo tsil a si gnirts A"
reverseList "Sigh of relief"
-&amp;gt; "feiler fo hgiS"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Detour: Q's &lt;code&gt;reverse&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;I mentioned earlier that Q already has a &lt;code&gt;reverse&lt;/code&gt; function. What does it look like? We can type the function name in the Q prompt to find out:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverse
-&amp;gt; |:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Is &lt;code&gt;|:&lt;/code&gt; considered one operator? Is it two? Why isn't there an argument in the function body? Is &lt;code&gt;|:&lt;/code&gt; meant to precede a list or succeed it? So many questions. We could answer the last one:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|: 1 2 3
-&amp;gt; `

1 2 3 |:
-&amp;gt; 3 2 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since &lt;code&gt;|: 1 2 3&lt;/code&gt; returns a signal, looks like the correct way to use &lt;code&gt;|:&lt;/code&gt; is definitely the second one. A cursory look at the various Q operators doesn't yield any answers as to how &lt;code&gt;|:&lt;/code&gt; is interpreted. Maybe it's an operator (or two operators) coming from &lt;code&gt;k&lt;/code&gt;, a language upon which Q is based. I have no difficulty putting this off for later (apologies if you do!).&lt;/p&gt;

&lt;h2&gt;
  
  
  Tail Recursion
&lt;/h2&gt;

&lt;p&gt;Our recursive function isn't &lt;a href="https://en.wikipedia.org/wiki/Tail_call"&gt;tail-recursive&lt;/a&gt;. Let's try to change it into one. We need some sort of an accumulator which will keep a running state of the reversed sublist. Once we get to a list of one element, we will simply join that element with the reversed sublist and make it the new head of the list. The following is a dry-run to demonstrate what I mean:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList 1 2 3 -&amp;gt; call reverseList[2 3] with accumulator set to 1
reverseList 2 3 -&amp;gt; call reverseList 3 with accumulator set to 2 1
reverseList 3 -&amp;gt; end of list, return 3 joined with accumulator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's our tail-recursive function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list;acc] 
    $[1 = count list;
        list,acc;
        reverseList[1 _ list;(1#list),acc]
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Tail recursion makes the function slightly more difficult to follow, but it does go easy on the stack.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList[1 2 3;()]
-&amp;gt; 3 2 1
reverseList[`a`b`c`d;()]
-&amp;gt; `d`c`b`a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice the ugly &lt;code&gt;()&lt;/code&gt; that we need to pass as the initial value of the accumulator - &lt;code&gt;()&lt;/code&gt; denotes an empty list. We can clean up the function by hiding the &lt;code&gt;()&lt;/code&gt; in an inner function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list] reverseListInner[list;()]}
reverseListInner: {[list;acc] 
    $[1 = count list;
        list,acc;
        reverseListInner[1 _ list;(1#list),acc]
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our solution obviously isn't as succinct as Q's, but we did learn the &lt;code&gt;$&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt;, and &lt;code&gt;#&lt;/code&gt; operators along the way.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList 1 2 3
-&amp;gt; 3 2 1
reverseList 1
-&amp;gt; ,1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ugh, looks like our function returns a list of one item when it's passed that item as an argument. Not really what we want. We could handle that case in our outer function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list] 
    $[1 = count list;
        list;
        reverseListInner[list;()]
    ]
}

reverseList 1
-&amp;gt; 1
reverseList 1 2 3
-&amp;gt; 3 2 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To sum up, here's our solution:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {[list] 
    $[1 = count list;
        list;
        reverseListInner[list;()]
    ]
}
reverseListInner: {[list;acc] 
    $[1 = count list;
        list,acc;
        reverseListInner[1 _ list;(1#list),acc]
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It's ugly to make the same check (&lt;code&gt;1 = count list&lt;/code&gt;) in two places for different purposes, but I'll call it a day at this point.&lt;/p&gt;

</description>
      <category>kdb</category>
      <category>q</category>
      <category>list</category>
      <category>reverse</category>
    </item>
    <item>
      <title>Koping with KDB 001: List Reversal</title>
      <dc:creator>Ali Faizan Kazmi</dc:creator>
      <pubDate>Mon, 24 Dec 2018 16:39:54 +0000</pubDate>
      <link>https://dev.to/alifaizankazmi/koping-with-kdb-001-list-reversal-3079</link>
      <guid>https://dev.to/alifaizankazmi/koping-with-kdb-001-list-reversal-3079</guid>
      <description>&lt;h5&gt;
  
  
  Estimated reading time: 10 minutes
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Preface&lt;/strong&gt;: Hi y'all (and Merry Christmas)! Welcome to the first of a series of posts about &lt;a href="https://code.kx.com/q/"&gt;kdb+&lt;/a&gt; - a high-performance columnar database which, coupled with its query language &lt;em&gt;Q&lt;/em&gt;, is used extensively in some areas of FinTech. More recently, kdb+ has also been applied to &lt;a href="https://kx.com/blog/detection-of-exoplanets-at-nasa-fdl-with-kdb/"&gt;machine learning&lt;/a&gt; outside of FinTech.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My narrative technique can be non-linear at times: e.g., a section's title may not make sense unless you've read some or all of the section; it may not be obvious from a code snippet as to what I'm trying to achieve; or I may build up to a solution only to discard it in the next instant. This is by design. If this causes any inconvenience to you while reading then let me know and I'll be happy to go for a more linear approach.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lastly, there is an inbuilt function in Q to reverse a given list but we're not gonna use it. We'll come up with our own solution instead and learn a few things along the way.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the first rules I learned when studying Q/kdb+ was that the Q/kdb+ interpreter evaluates expressions from right to left, even though expressions are typed left-to-right. For instance, the result of the following expression:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2*3+3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;is &lt;code&gt;12&lt;/code&gt;, since the addition operator and its operands are evaluated first and the result of the addition in turn becomes the second operand for the * operator.&lt;/p&gt;

&lt;p&gt;Sound simple enough? I certainly thought so, until my forehead landed on my palm a few minutes later. &lt;/p&gt;

&lt;h2&gt;
  
  
  Recursion?
&lt;/h2&gt;

&lt;p&gt;I was attempting to define a simple function that would return true if it were passed a list as its argument:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isList: {[object] type object = 7h}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;A breakdown of the above follows for the uninitiated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;: is the assignment operator&lt;/li&gt;
&lt;li&gt;A function definition is encapsulated in curly brackets&lt;/li&gt;
&lt;li&gt;A function's parameters are encapsulated in square brackets inside the function definition&lt;/li&gt;
&lt;li&gt;= is the equality operator (since : takes care of assignment)&lt;/li&gt;
&lt;li&gt;Each datatype in kdb is represented by a special 16-bit integer. In this case, we want the integer that represents the list datatype - 7h (well, &lt;a href="http://code.kx.com/q/ref/datatypes/"&gt;not exactly&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Generally, a non-niladic function in kdb is invoked by passing it one or more arguments within square brackets. This is optional for monadic functions, though - as seen above in case of the &lt;code&gt;type&lt;/code&gt; function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Back to our function definition. If, like me, you didn't immediately notice what was wrong then here it is: the expression&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type object = 7h
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;is evaluated right-to-left, which means the expression is not performing the intended type check at all! From right to left, this expression contains two inner expressions evaluated in the following order:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type (object = 7h)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, &lt;code&gt;object = 7h&lt;/code&gt; evaluates to a boolean value (&lt;code&gt;0b&lt;/code&gt; or &lt;code&gt;1b&lt;/code&gt;) which is then served as the argument to the &lt;code&gt;type&lt;/code&gt; function. No wonder, then, that we get the following result when we invoke our function with a list of numbers:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isList 1 2 3
-&amp;gt; -1h
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;-1h&lt;/code&gt; is the numerical representation of the boolean datatype. &lt;/p&gt;




&lt;p&gt;As an aside, lists in kdb are typically enclosed in round brackets and a semi-colon is used to separate list items. However, round brackets and semi-colons can be dropped for a list of &lt;em&gt;atoms&lt;/em&gt; (known as primitives in languages like Java) where each atom is of the same datatype.&lt;/p&gt;




&lt;p&gt;Here is the corrected version of the function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isList: {[object] 7h = type object}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which gives us the expected result when invoked with a list of numbers:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isList 1 2 3
-&amp;gt; 1b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Why do I want such a function, you ask? Because I would like to ultimately define a function that takes a list as an argument and returns another list with its elements in the reverse order. One way we could achieve a list reversal is via recursion and for recursion to work (or, more precisely, to stop!) we need a base case. In this example, the base case would be the point where we have reduced the provided list to its last element. &lt;/p&gt;

&lt;h2&gt;
  
  
  Really? Recursion?
&lt;/h2&gt;

&lt;p&gt;Wait, do we really need recursion? Given that Q is an array programming language (among other things), could we not just pass a list of indices as an argument to our list in order to obtain a new reversed list? Of course, we'll need to make sure the list of indices is in reverse, too. But that shouldn't be difficult. To generate a list of indices, we can make use of the &lt;code&gt;til&lt;/code&gt; function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;til 3
-&amp;gt; 0 1 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function can be used to obtain a list of valid indices for a list by using the &lt;code&gt;count&lt;/code&gt; function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list: 1 2 3
til count list
-&amp;gt; 0 1 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, if we could reverse the list of indices then our problem should be solved:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list 2 1 0
-&amp;gt; 3 2 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The following is one strategy to reverse this list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The maximum valid index is &lt;code&gt;-1 + count list&lt;/code&gt; (Am I the only one who came up with &lt;code&gt;count list - 1&lt;/code&gt; first?)&lt;/li&gt;
&lt;li&gt;Subtracting a list of indices that is sorted in ascending order from the maximum valid index will yield a list that contains the same indices in reverse&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our case:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;indices: til count list
2 - indices
-&amp;gt; 2 1 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Great, now we need to wrap all of this into a function. We can't name our function &lt;code&gt;reverse&lt;/code&gt; since that name is already reserved (for a function the definition of which I'm too scared to find out just yet). Here's the function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList: {
    [list] list (-1 + count list) - til count list
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This seems to work correctly:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList 1 2 3
-&amp;gt; 3 2 1

reverseList "level"
-&amp;gt; "level"
reverseList 1 0 1
-&amp;gt; 1 0 1
reverseList "Eva, can I stab bats in a cave?"
-&amp;gt; "?evac a ni stab bats I nac ,avE"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Apologies, I got carried away with palindromes.&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reverseList 110b /List of booleans (incidentally, this is a comment)&lt;br&gt;
-&amp;gt; 011b&lt;br&gt;
reverseList 2018.09.23 2018.09.22 2018.09.21&lt;br&gt;
-&amp;gt; 2018.09.21 2018.09.22 2018.09.23&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Is our solution good enough?&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Hardly. Why should we have to create a new list (i.e., the list of indices in reverse) in order to reverse a given list? Also, I don't like having to use parantheses to force the evaluation of an expression first - a sign that I'm still wet behind the ears vis-à-vis Q's right-to-left evaluation. Perhaps I should take a stab at recursion after all, or find a better solution as I continue to study Q and slowly shed the yoke of imperative/non-array programming paradigms.&lt;/p&gt;

</description>
      <category>kdb</category>
      <category>q</category>
      <category>list</category>
      <category>reverse</category>
    </item>
  </channel>
</rss>
