
K7 Tutorial - chrispsn
https://cs.nyu.edu/cs/faculty/shasha/papers/tutorial
======
sannysanoff
For curious, several facts i noticed by running anaconda distributed shakti:

1) k7 implementation obviosly is completely different codebase from current
kdb+. I ran the following query on kdb+ and k7

    
    
      a1:1000000?1000000;b1:1000000?1000000;
      \t select #a by b from (+`a`b!(a1;b1));
    

And difference seems to be in order of magnitude (k7 is 650 msec, kdb+ is 77
msec), also on k7, time increases for subsequent execution of same query ==>
memory leak? Looks like it's very early stage.

2) null number (0N) and infinity are now represented as non-ascii symbols Ø
and ∞, also parsed as such.

3) type operator (@) returns symbols (`i, `j) for ints and longs etc, was
returning shorts before. Interesting how do we distinguish arrays/scalars now.

4) default numeric type (e.g. 12345) is now int, was long.

5) entering overflow numeric literal returns Ø, was throwing exception.

6) k in anaconda is not stripped 675K linux x64 executable, stripped is 220K,
while kdb+ is 657K as sold.

~~~
SifJar
> 3) type operator (@) returns symbols (`i, `j) for ints and longs etc, was
> returning shorts before. Interesting how do we distinguish arrays/scalars
> now.

Upper/lower case:

    
    
       @1
      `i
       @1 2
      `I
       @`a
      `n
       @`a`b
      `N

~~~
sannysanoff
I saw that, but how do you make simple check if it is uppercase?

~~~
SifJar
I guess you can check the ASCII value of the char:

    
    
       li:{(*$@x)within "AZ"}
       li 1
      0
       li 1 2 3
      1
    

Or can have a list of uppercase chars as names & check with 'in':

    
    
       li2:{(@:y)in x}[`$,:'`c$"A"+!26]
       li2 1 2 3
      1
       li2 1
      0
    

'li2' seems to be considerably faster:

    
    
       \t:10000 li 1 2 3
      13
       \t:10000 li2 1 2 3
      6
    

There may well be a better way though

~~~
sannysanoff
this seems not to be in the spirit of language. Testing if type is array or
not is so common, and adding this function (li, li2) to "standard library"
would introduce "standard library". Maybe we overlooked some other obvious
solution.

------
toastking
A concept in K that I wish more languages had is indexing with a list of
indices. It always blows peoples' minds when I tell them how sorting works in
K. You use grade (<) to return the indices that will sort the array if indexed
in that order. Then you pass in that list as the indices to the list. They do
it in the string examples in this tutorial.

~~~
faitswulff
How does grade work? Can you customize how it ranks results?

~~~
i_don_t_know
If you want to rank results differently, you'd apply a function to each
element of the original vector that computes the rank / weight of that
element. Then you sort by weights, and apply the result to the original
vector. Something like:

input[<{...compute weight of element x...}'input]

~~~
toastking
This is also how filtering works, you map a function that returns 0 or 1 over
the list. Then you can call where (&) and it will give you the indices where
the function evaluates true. Index by that and you'll filter a list.

------
yiyus
It's a very good tutorial (it's not easy to find information about k7), but it
is a bit outdated. For example, % is inverse and not sqrt now, and it is not
possible to evaluate parsed trees with !. I am sure the differences are small,
but be prepared to be surprised with some descriptive error messages like:
'nyi

~~~
pvitz
'nyi = not yet implemented

~~~
yjftsjthsd-h
And that's very obvious once it's been pointed out, but how long would it take
a beginner to figure that out?

~~~
yiyus
A beginner is going to have a bad time with any language. Sure, 'nyi is not
very descriptive, but an error message is not the place to teach total
beginners. Unless you put some effort into learning a language (or already
know something similar), compiler errors will be difficult to figure out
anyway.

The most blatant example is the infamous "discards qualifiers" error but, for
example, in a programming language with nice and descriptive error messages
like Go, you will have a hard time figuring out what a interface type error is
about until you actually read the manual and learn what an interface is.

~~~
smacktoward
A good error message doesn't have to be descriptive, but if it isn't, it
absolutely _must_ be Googleable -- i.e. unique enough so that someone who
doesn't understand what it means can paste it into a search engine and find
out.

If I Google "'nyi", I get a bunch of results about the New York Islanders
hockey team. Not helpful. If I try to be more specific and Google "'nyi k7",
the closest thing to a helpful result I get is a link to this discussion on
HN.

------
avmich
What's important is that APL language family isn't a domain-specific one,
suitable for some particular tasks in a narrow niche. To the contrary, they
are languages of computations, allowing rather short paths from thought to
implementation of an algorithm, compared to other languages.

I'm sure K knowledge and experience ensure the practitioner powerful tools for
wide variety of problems in many areas.

~~~
a-saleh
Still, for me it is quite hard to imagine what would I create with APL.

Usually, when trying a language, my instinct is to implement a web-service. Or
a simple gui-app, with some library.

With APL, I am looking at it and wonder. I looks powerful. But what should I
do with it? Make a compiler? A database? How does I/O even work there?

~~~
toastking
In K, usually the I/O primitives you have are the "colon" operators, stuff
like 4:.

------
pvitz
The mentioned gitlab repo doesn't seem to exist anymore. One would have to
download kdb+ from kx, but I am not sure if this wouldn't be K4 only.
Alternatively, one could play with oK (K6) which would also give you the nice
graphical iKe.

~~~
chrispsn
K7 is now available as a trial from Shakti Software, Arthur Whitney’s new
company. [https://anaconda.org/shaktidb](https://anaconda.org/shaktidb)

There is a Google Group:
[https://groups.google.com/forum/#!forum/shaktidb](https://groups.google.com/forum/#!forum/shaktidb)

~~~
ah-
I didn't really follow that development, what led to the creation of Shakti?

~~~
4thaccount
Not really sure... people started talking about it on the mostly dead /r/apljk
subreddit last week which caught my attention. It appears Arthur Whitney left
Kx Systems and started his own company again. I'm immediately suspicious when
I see "blockchain" in something, but Arthur is a bit of a legend. I don't
fully understand why it is bundled as an Anaconda (scientific and data
analysis oriented distribution of Python), but I assume a library is utilized
there for machine learning or something like that (hopefully someone can
comment and tell us what). You can type in "/" in the REPL to bring up the
help menu showing the various symbols and what they mean monadically (when
taking one argument) or dyadically (taking two arguments). As usual though,
their website seems to have virtually zero documentation like they want to
stay obscure. I'd love if someone in the industry that can afford to use these
products can tell me what Shakti looks like it would be used for. Is it the
same as kdb+ mostly with a few added features?

~~~
mhd
Yeah, the mere mention of blockchain/crypto was also the reason why my
interesting in Red waned suddenly...

Still waiting for kOS, too.

~~~
4thaccount
Haha. Same here for Red. I understand why they did it (it seems like they
wanted blockchain and crypto currency to be their killer app), but I'm worried
it was just a costly detour. That is their decision of course though and best
of luck. The other thing they're doing (if I understand correctly) is creating
a second code optimizer that is a paid product. So the default compiler
created slower code than the commercial one I think.

I'm also still waiting on kOS, but I'll never be able to afford it, so not
sure why I'm waiting on it.

------
vadiml
K7 like it's ancestor APL is extremely powerful language, whit great signal to
noise ratios. However it is kind of "write-only" language.. When trying to
read a code not written by you (or written by you couple of monthes ago) you
struggle to understand it...

~~~
icen
I think that this is a matter of practice. I write k/q almost daily, and don't
struggle to read other people's code, or my own.

Most code isn't clever oneliners, and is quite readable.

~~~
anonu
Agree with this. The problem is that there's no widely adopted style guide
with k or q. A lot of code you find on the web is written in a minified
fashion. At least put a new line and indent whenever you can.

------
pmontra
The language looks generally good for its goal, except these two operators

    
    
         |/y / maximum
     
         &/y / minimum
    

Those two characters have no connection with the operation they stand for and
seem to be randomly picked from the available symbols on the keyboard. Why not
simply max y and min y, which anybody can read and understand?

~~~
i_don_t_know
At least for boolean operations, | (or) can be thought of as the maximum of a
vector of 0s (false) and 1s (true). Likewise, & (and) is the minimum of a
vector of 0s (false) and 1s (true).

But I don't know if that was the motivation / justification for naming these
operators / functions.

~~~
uryga
if you squint a bit, `x or y` returns the one that's greater, i.e. `0 or 1 ==
1`; and `x and y` returns the one that's smaller, i.e. `0 and 1 == 0`. so
min/max kind of are a generalization of and/or

------
beaumayns
I was hoping we'd finally get proper lexical scope in this version of K. Alas,
seems to not be the case.

~~~
yiyus
There are a few expected (or, better said, rumored) features that have been
left out. Some of them will eventually come (views), some of them probably
don't (like using the unicode symbol for sqrt). I was surprised when I saw in
nsl a shift-reduce parser that took into account operator precedence, but it
looks like it is not being used. I'd like to have lexical scoping too, but due
to how function values work, I see it very unlikely.

~~~
beaumayns
Unlikely due to how they're implemented, or their semantics?

~~~
chrispsn
According to this thread, it makes IPC harder:
[https://www.reddit.com/r/apljk/comments/82vcs4/comment/dyxb0...](https://www.reddit.com/r/apljk/comments/82vcs4/comment/dyxb0mv)

------
mark_l_watson
I liked the example of 1 line of code generation a table of random stock data.

But, this is not free or open source?

~~~
toastking
There is an open source port of K called Kona:
[https://github.com/kevinlawler/kona](https://github.com/kevinlawler/kona)

~~~
codetrotter
It’s not a port, it’s a separate implementation. The difference in meaning
between these two words is sufficiently important that it’s worth pointing out
IMO.

Porting software means to adapt an existing piece of software to a different
computing environment.

Ports are based on source code and/or assets from the original piece of
software that is being ported.

As such a port would be derivative work, meaning that the authors would not be
able to release their software without permission from the copyright holder(s)
of the software that was being ported.

------
quickthrower2
Can any comment how this compares to R? Seems to have similar capabilities.

~~~
et2o
The syntax seems vaguely similar, but I don't really see other commonalities.
R's strength is it's incredible ecosystem.

------
asimjalis
What are the financial applications that this is used for? Does anyone have
specific examples?

~~~
bwanab
In the 1990s Morgan Stanley's full suite of fixed income portfolio management
and trading applications were written in APlus which was another of Arthur
Whitney's APL variants. When I joined, MS was moving away from it, but the
developers who'd been using it were very much in the "you'll take APlus from
my cold, dead fingers" camp.

------
didsomeonesay
Interesting previous discussion of K programming languages (up to K5):

[https://news.ycombinator.com/item?id=16500908](https://news.ycombinator.com/item?id=16500908)
(2005)

------
surajs
Gitlab link to download it is a 404!?

------
ngcc_hk
Difference between J7 and K7?

~~~
4thaccount
J and K are different languages. They are similar in that both use standard
ascii keys and are in the APL family of languages. The author of K "Arthur
Whitney" wrote part of a prototype with Roger Hui for the J interpreter or so
I think. The primary author of J is none other than Ken Iverson (the Turing
award winner and designer of the original APL) who wanted a free version of
APL for the masses where people wouldn't complain about the weird non ASCII
APL symbols. It also primarily uses a tacit function train style of
programming like data flow languages where you don't even necessarily need
variables (pretty cool). This tacit coding style has since been added to
Dyalog APL where you can see folks like Aaron Hsu build some pretty cool
applications using it. I'd imagine K is more performant though than J and the
source is famously small (just a few C files). J has a lot more stuff baked in
like Qt support. They both have a database library Jd (for J) and Kdb+ (for
K). Kdb+ is a fancy SSD based database which uses K and a SQL like DSL called
Q. Its performance is pretty darn good and used in Finance. I don't know
enough about Jd.

~~~
etatoby
_> [K's] source is famously small (just a few C files)_

Yes, but they look like this:

[https://github.com/tangentstorm/j-incunabulum/blob/master/ji...](https://github.com/tangentstorm/j-incunabulum/blob/master/ji.c)

Call me crazy, but I'm wary of code written like this.

~~~
rbonvall
I get the same gut reaction as you, but I tried reformatting the code a little
bit and it's not _that_ terrible.

From the very little APL I've learnt, I know that operators always have either
one parameter (named ω) or two (named α and ω), and their inputs and outputs
are always arrays.

If you need to write a lot of such functions, it makes sense to define some
macros to help you:

    
    
        #define V1(f) A f(w)A w;      // create one-arg function
        #define V2(f) A f(a,w)A a,w;  // create two-arg function
        #define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
    

iota is the APL equivalent of Python's range function:

    
    
        V1(iota) {            // Define one-arg function iota.
            I n = *w->p;          // Get the value of the ω argument.
            A z = ga(0, 1, &n);   // Allocate output array.
            DO(n, z->p[i] = i);   // Assign increasing integers.
            R z;                  // Return the result.
        }
    

The plus function adds two arrays:

    
    
        V2(plus) {            // Define two-arg function plus.
            I  r = w->r,          // Get the rank of ω.
              *d = w->d,          // Get a pointer to ω's data.
               n = tr(r,d);       // Get tne size of ω's data.
            A z = ga(0, r, d);    // Allocate output array.
            DO(n, z->p[i] = a->p[i] + w->p[i]);
                                  // Add corresponding values of α and ω
            R z;                  // Return the result.
        }
    

Personally I cannot tolerate the lack of whitespace, but the APL guys are
known to like to see their entire programs in one screenful. I can understand
that some people like to write code like this.

------
maxpetis
Yeah I could really use that. I'd like to take advantage of its enhanced BCLK
capabilities but every time I mess with it it doesn't POST.
[https://www.assignmentland.co.uk/](https://www.assignmentland.co.uk/)

------
svnpenn
is it a private repo? this is asking me to login

[https://gitlab.com/k7db/k](https://gitlab.com/k7db/k)

~~~
anonu
I think so - I can't clone

------
patrickg_zill
Trying to learn how to use A+ from aplusdev.org to do some simple work for my
personal use. A previous language designed by Arthur Whitney and GPL also.
Included in Debian derived distros btw.

~~~
etatoby
I would highly recommend GNU APL over A+.

GNU APL is a full-fledged, modern implementation of the official ISO APL 2
standard, supporting things like Unicode and modern terminals, and is
currently maintained by very friendly people. While A+ is a very old, partial
implementation of APL that requires a dedicated font and 8-bit encoding to
work, and has not been maintained for decades. (IIRC)

Documentation for APL 2 can be found online, but I would recommend getting a
second hand copy of the classic Gilman Rose book. I bought mine online for a
few dollars.

~~~
patrickg_zill
Is there a particular edition of the Gilman Rose book you would recommend?

~~~
etatoby
Sorry for the late reply. I seem to have the 1976 edition (I didn't think of
checking the edition before buying mine! DOH!) I would say it was good enough
for learning the language, although the references to mainframes and terminals
are obsolete. (Picturesque, if you like that sort of thing, but obsolete.) If
you can get the latest edition (1991) it's probably better.

I ended up buying that book because from all the online resources I was able
to find at the time, I failed to understand the _gist_ of the language, or how
you are supposed to think in order to solve problems in APL. Which is
profoundly different from all other languages, and which the book taught very
well. I've also been using paper books to learn programming since forever, so
it's a workflow I'm used to.

That being said, there are some free resources that you could try first:

[http://misc.aplteam.com/robertson/APL1&2.pdf](http://misc.aplteam.com/robertson/APL1&2.pdf)
\-
[http://misc.aplteam.com/robertson/APL3&4.pdf](http://misc.aplteam.com/robertson/APL3&4.pdf)
\-
[https://www.dyalog.com/uploads/documents/MasteringDyalogAPL....](https://www.dyalog.com/uploads/documents/MasteringDyalogAPL.pdf)

The latter is about Dyalog APL, a dialect of APL, so not all of it will be
applicable to GNU APL.

About the font, any Unicode font will do, but for graphical consistency I
would recommend downloading this one. You can use it with any modern terminal
application:

[https://www.dyalog.com/uploads/files/download.php?file=fonts...](https://www.dyalog.com/uploads/files/download.php?file=fonts_and_keyboards/Apl385.ttf)

About the keyboard, if you are on Linux you can just add one of several APL
variants to your existing national keyboard. You will have to designate an APL
key (eg. the useless menu key) to use as a special alt-like key to input APL
symbols. If you're not comfortable typing blindly, you can buy a keyboard with
APL symbols on it, or alternatively buy or print stickers to apply to yours.

I spent an entire summer learning APL for fun (that was way before GNU APL
existed, so I had to struggle with various proprietary interpreters, either
freeware or demoware, on top of the weird language + font + keyboard!) I then
went on to compete in Code Golf challenges and other random things using it
and had a blast!

I still miss APL. IMHO none of the successors (J, K) come close to the beauty
of its symbols. It's also a very different way of writing algorithms
(sometimes called "multi-dimensional array programming") that is not found in
modern programming languages.

Feel free to contact me (username + gmail) for any questions you may have.

------
ne01
Off topic, but is it just me, or do you also dig the simple text format of the
website? -- so easy to read!

~~~
jchw
Wish it didn't have manual line breaks, though; it's kind of terrible on
mobile.

~~~
Stratoscope

      The first sentence
      reminds me a bit of the story of
      Mel.
    

[http://catb.org/jargon/html/story-of-
mel.html](http://catb.org/jargon/html/story-of-mel.html)

------
twohoursprog
Any good programmer should be able o learn to program k7 at the level of this
tutorial in two hours.

