JSLover
JSLover

Reputation: 165

Using the K inside of KDB+ / Q and conditional branching specifically

I figure, to be a real KDB expert, I should learn K, right? So I can write some fast functions and understand how things actually work, etc.?

I found this definition of factorial that does not work, even though it was an example in K-Lite Ref Manual.

fac1: {:[x>1; x * fac[x-1]; 1]}

I modified it to use if rather than conditional (:) and it works.

fac2: {if[x>1; :x * fac[x-1]]; 1}

Has the ":[a;b;c]" syntax gone away? What replaced it?

Is there a cheat sheet for the actual version of K that is underlying KDB+ that I can get a copy of?

Please?

I'm actually kind of falling in love with K. Get a load of Quicksort in K:

https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#K

OMG! Makes other languages seem SO long winded!

Upvotes: 2

Views: 949

Answers (1)

Connor Gervin
Connor Gervin

Reputation: 946

Try using $ (if-else) instead of :

http://code.kx.com/q/ref/control/#cond

Here is a cheat sheet for learning q/kdb+. https://github.com/KxSystems/kdb/blob/master/d/primer.htm

IMO it will much easier to start learning q as there are much more resources available. i.e code.kx

Example:

fac:{$[x>1; x * fac[x-1]; 1]}

Upvotes: 2

Related Questions