-
Notifications
You must be signed in to change notification settings - Fork 9
/
k9Simples.html
5645 lines (4947 loc) · 178 KB
/
k9Simples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- April 15, 2023
Copyright (C) 2020 John Estrada -->
<title>Shakti (k9) tutorial</title>
<meta name="description" content="k programming tutorial, Shakti (k9) tutorial">
<meta name="keywords" content="Shakti (k9) tutorial">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2any">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="#Top" rel="start" title="Top">
<link href="#Introduction" rel="next" title="Introduction">
<style type="text/css">
<!--
/* spacing */
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
.cartouche{
border: none;
background: lightgrey;
}
table, th, td{
table-layout: auto;
border-collapse: collapse;
border: 1px solid blue;
font-family: monospace;
}
th, td {
padding: 5px;
font-family: monospace;
}
tr:nth-child(1) {
/* background-color:lightsteelblue; // Replace it with your desired color */
}
.menu{
border: none;
}
table.menu td{
padding: 1px;
border: none;
font-family: sansserif;
}
table.menu th{
padding: 1px;
border: none;
font-family: sansserif;
}
-->
</style>
</head>
<body lang="en">
<h1 class="settitle" align="center">Shakti (k9) tutorial</h1>
<div class="top" id="Top">
<div class="header">
<p>
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a> </p>
</div>
<span id="k9_003a-Manual"></span><h1 class="top">k9: Manual</h1>
<p>This manual is for Shakti (k9) build 2023.03.09.<br>
</p>
<p>The Shakti programming language is the work of Arthur Whitney and the team at Shakti. The language comes from a lineage of similar languages going back to APL. It’s also known as k9 given the similarities to k, k2, ..., k6, k7 and the fact that Arthur also wrote those. k9 is still under development so expect a bit of syntax change and likely even more improvements.
</p>
<p>Learning k9 means code like <code>{x@(!#x)+\!#y}</code> is clear and actually prefered to something much longer and verbose. You’ll have to go a bit down the rabbit hole before it starts to come together but once it does you’ll probably be happy you gave it a chance and end up learning a few things. You’ll understand how fast k9 can be in processing and analyzing data and how terse code is less likely to contain errors. Unfortunately you will be less likely to be content using more bloated and slow frameworks.
</p>
<p>Alas this manual is not as elegant as the k9 language in that the text version of this document is almost as big as the binary of the language.
</p>
<p>The built-in reference screen (<code>\</code>) containing all the k9 commands.
</p>
<p><tt>select <a href="#count">count</a> <a href="#first">first</a> last min max sum avg var dev .. by ..</tt><br>
<tt>in n_(rand) n@(multiply) n?(divide) n@n?(bar)</tt><br>
<tt></tt><br>
<tt>Verb monad Ad<a href="#Verb">verb</a> Type </tt><br>
<tt>+ + ' <a href="#each">each</a> <a href="#char">char</a> " ab" </tt><br>
<tt>- - / <a href="#cover">over</a> sym ``ab</tt><br>
<tt>* * \ <a href="#cscan">scan</a> <a href="#bool">bool</a> 011b </tt><br>
<tt>% <a href="#div">div</a> <a href="#int">int</a> 2 3 4</tt><br>
<tt>! <a href="#mod">mod</a> <a href="#where">where</a> <a href="#System">System</a> float 2 3e4 </tt><br>
<tt>& & <a href="#flip">flip</a> <a href="#load">\l</a> load -fixed 2.0 3.4</tt><br>
<tt>| | <a href="#reverse">reverse</a> <a href="#timing">\t</a> time -locus -74::40.7 </tt><br>
<tt>< < <a href="#asc">asc</a> <a href="#variables">\v</a> vars <a href="#z_002ed">z.d</a> <a href="#date">date</a> 2001.02.03 </tt><br>
<tt>> > <a href="#desc">desc</a> \w work <a href="#z_002et">z.t</a> <a href="#time">time</a> 12:34:56.789</tt><br>
<tt>= = freq <a href="#z_002eT">z.T</a> <a href="#datetime">datetime</a></tt><br>
<tt>~ ~ ~ </tt><br>
<tt>, , ,</tt><br>
<tt># <a href="#take">take</a> <a href="#count">count</a> I/O Class</tt><br>
<tt>_ <a href="#drop">drop</a> <a href="#first">first</a> 0' line <a href="#Expression">expr</a> :2+a</tt><br>
<tt>^ <a href="#cut">cut</a> <a href="#sort">sort</a> 1' char/stdout <a href="#User-Functions">func</a> f[a] 2+a</tt><br>
<tt>@ @ <a href="#type">type</a> 2' data/stderr </tt><br>
<tt>? <a href="#find">find</a> unique *3' set <a href="#List">list</a> (2;3.4) </tt><br>
<tt>$ <a href="#parse">parse</a> <a href="#str">str</a> *4' get <a href="#Dictionary">dict</a> {a:2 3}</tt><br>
<tt>. <a href="#dict">dict</a> <a href="#value">value</a> *5' <a href="#ffi">ffi</a> <a href="#Tables">table</a> [a:2 3]</tt><br>
</p>
<ul class="section-toc">
<li><a href="#Introduction" accesskey="1">Introduction</a></li>
<li><a href="#Verb" accesskey="2">Verbs</a></li>
<li><a href="#Adverb" accesskey="3">Adverbs</a></li>
<li><a href="#Noun" accesskey="4">Noun</a></li>
<li><a href="#List" accesskey="5">List</a></li>
<li><a href="#Dictionary" accesskey="6">Dictionary</a></li>
<li><a href="#User-Functions" accesskey="7">User-defined Functions</a></li>
<li><a href="#Expression" accesskey="8">Expression Evaluation</a></li>
<li><a href="#Named-Functions" accesskey="9">Named Functions</a></li>
<li><a href="#Knit-Functions">Knit Functions</a></li>
<li><a href="#I_002fO">I/O and Interface</a></li>
<li><a href="#FF">Foreign Functions</a></li>
<li><a href="#Tables">Tables</a></li>
<li><a href="#kSQL">kSQL</a></li>
<li><a href="#System">System</a></li>
<li><a href="#Control-Flow">Control Flow</a></li>
<li><a href="#Temporal-Functions">Temporal Functions</a></li>
<li><a href="#Errors">Errors</a></li>
<li><a href="#Examples">Examples</a></li>
<li><a href="#Benchmark">Benchmark</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ul>
<hr>
<div class="chapter" id="Introduction">
<div class="header">
<p>
Next: <a href="#Verb" accesskey="n" rel="next">Verbs</a>, Previous: <a href="#Top" accesskey="p" rel="prev">k9: Manual</a>, Up: <a href="#Top" accesskey="u" rel="up">k9: Manual</a> </p>
</div>
<span id="Introduction-1"></span><h2 class="chapter">1 Introduction</h2>
<p>The k9 programming language is designed primarily for the analysis of data. It may surprise new users with two rather different paradigms, (1) fast data analysis and (2) concise syntax. After you become familiar with the language, these features will both seem normal and intuitive. Also, going back to slow and verbose programming will be surprisingly difficult.
</p>
<ul class="section-toc">
<li><a href="#Going-fast" accesskey="1">Going fast</a></li>
<li><a href="#Going-concise" accesskey="2">Going concise</a></li>
<li><a href="#Get-k9_002e" accesskey="3">Get k9.</a></li>
<li><a href="#Help-_002f-Info-Card" accesskey="4">Help / Info Card</a></li>
<li><a href="#rlwrap" accesskey="5">rlwrap</a></li>
<li><a href="#REPL" accesskey="6">REPL</a></li>
<li><a href="#Simple-example" accesskey="7">Simple example</a></li>
<li><a href="#Document-formatting-for-code-examples" accesskey="8">Document formatting for code examples</a></li>
<li><a href="#k9-idiosyncracies" accesskey="9">k9 idiosyncracies</a></li>
</ul>
<div class="section" id="Going-fast">
<h3 class="section">1.1 Going fast</h3>
<p>Imagine you have a small, on-disk, 100 million row database containing a time-series with two float values at each time. Additionally this data could be split in three different tables covering different measurements. Here’s how fast k9 can read in the data from disk and compute a statistic, average difference over each table, which uses each and every row.
</p>
<p><b>This section requires 2: a feature only in the enterprise version of Shakti. If that is not available then use the section below with 1:</b>
</p>
<div class="example">
<pre class="example"> bash-3.2$ ./k
2021.xx.xx 17GB 4core (c) shakti
\t q:2:`q;{select s:avg a-b from x}'q[!q]
884
</pre></div>
<p>That’s 884 ms to read the data in from disk and compute over all the 100 million values. The data read is the biggest bit. If the data were already in memory then the computation would be faster:
</p>
<div class="example">
<pre class="example"> \t {select s:avg a-b from x}'q[!q]
217
</pre></div>
<p>217 ms, not bad for 100 million calculations.
</p>
<p>The code to generate the on-disk database is presented below. Speed of course will depend on your hardware so times will vary.
</p>
<div class="example">
<pre class="example"> nf:d+*|d:(|-d),d:683 954 997 1000;
T:^`t ?[;_8.64e7]@
B:100++\1e-2*-3+nf bin/:?[;*|nf]@
S:?[;1e-2*2,2,8#1]@
L:{select t,b,a:b+s from +`t`b`s!(T;B;S)@'x}
q:`eurusd`usdjpy`usdchf!L'_60e6 20e6 20e6
`q 2:q
</pre></div>
<p><b>This section requires 1: a feature in all versions of Shakti.</b>
</p>
<div class="example">
<pre class="example"> bash-3.2$ ./k
2021.xx.xx 17GB 4core (c) shakti
\t select s:avg a-b from q:`csv?1:"q.csv"
832
</pre></div>
<p>That’s 832 ms to read the data in from disk and compute over all the 10 million values. The data read and csv conversion process are the biggest bits.
</p>
<p>Here is the code to generate the q.csv on-disk file. Note in this example only 10 million lines are generated versus the 100 million lines in the previous example using 2:
</p>
<div class="example">
<pre class="example"> nf:d+*|d:(|-d),d:683 954 997 1000;
T:^`t ?[;_8.64e7]@
B:100++\1e-2*-3+nf bin/:?[;*|nf]@
S:?[;1e-2*2,2,8#1]@
L:{select t,b,a:b+s from +`t`b`s!(T;B;S)x'}
q:L[_10e6]
"q.csv"1:`csv@q
"q.csv"
</pre></div>
</div>
<div class="section" id="Going-concise">
<h3 class="section">1.2 Going concise</h3>
<p>The k9 language is more closely related to mathematics syntax than most programming lanauges. It requires the developer to learn to “speak” k9 but once that happens most find an ability to speak quicker and more accurately in k9 than in other languages. At this point an example might help.
</p>
<p>In mathematics, “3+2” is read as “3 plus 2” as you learn at an early age that “+” is the “plus” sign. For trival operations like arithmetic most programming languages use symbols also. When moving beyond arithmetic, most programming lanauges switch to words while k9 remains with symbols. As an example, to determine the distinct values of a list most programming languages might use a syntax like <code>distinct()</code> while k9 uses <code>?</code>. This requires the developer to learn how to say a number of symbols but once that happens it results in much shorter code that is quicker to write, easier to inspect, and easier to maintain.
</p>
<p>This should not be surprising. In arithmetic, which do you find easier to understand?
</p>
<div class="example">
<pre class="example">/ math with text
Three plus two times open parenthesis six plus fourteen close parenthesis
/ math with symbols
3+2*(6+14)
</pre></div>
<p>In code, if you’re new to k9 then it’s unlikley you can understand the second example.
</p>
<div class="example">
<pre class="example">/ code with text
x = (0,12,3,11,3);y=5;
distinct_x = list(set(x));
sorted(i for i in distinct_x if i >= y)
/ code with symbols
x:0 12 3 11 3;y:5;
z@&y<z:?x
</pre></div>
<p>When you first learned arithmetic you likely didn’t have a choice. Now you have a choice whether or not you want to learn k9. If you give it a try, then you’ll likely get it quickly and move onto the power phase fast enough that you’ll be happy you gave it a chance.
</p>
</div>
<div class="section" id="Get-k9_002e">
<h3 class="section">1.3 Get k9.</h3>
<p><a href="https://shakti.com">https://shakti.com</a>
</p>
<p>k9 is available in two versions, standard (under download) and enterprise. The enterprise version has additional features indicated on the k9 help page and also indicated in this tutorial.
</p>
<p>Once downloaded you will need to change the file mode with the following commmand
</p>
<div class="example">
<pre class="example"> chmod +x k
</pre></div>
<p>On the mac if you then attempt to run this file you likely won’t succeed due to MacOS security. You’ll need to go to “System Preferences...” and then “Security and Privacy” and select to allow this binary to run. (You’ll have to have tried and failed to have it appear here automatically.)
</p>
<p>On linux (and macos if you have installed npm) one can download k from the command line via
</p>
<div class="example">
<pre class="example"> npm i @kparc/k -g
</pre></div>
</div>
<div class="section" id="Help-_002f-Info-Card">
<h3 class="section">1.4 Help / Info Card</h3>
<p>Typing <code>\</code> in a terminal window gives you a concise overview of the language. This document aims to provide details to beginning user where the help screen is a bit too terse. Some commands are not available in the basic version and thus marked with an asterisk, eg. *4: https get.
</p>
<div class="format">
<pre class="verbatim">select count first last min max sum avg var dev .. by ..
in n_(rand) n@(multiply) n?(divide) n@n?(bar)
Verb monad Adverb Type
+ + ' each char " ab"
- - / over sym ``ab
* * \ scan bool 011b
% div int 2 3 4
! mod where System float 2 3e4
& & flip \l load -fixed 2.0 3.4
| | reverse \t time -locus -74::40.7
< < asc \v vars z.d date 2001.02.03
> > desc \w work z.t time 12:34:56.789
= = freq z.T datetime
~ ~ ~
, , ,
# take count I/O Class
_ drop first 0' line expr :2+a
^ cut sort 1' char/stdout func f[a] 2+a
@ @ type 2' data/stderr
? find unique *3' set list (2;3.4)
$ parse str *4' get dict {a:2 3}
. dict value *5' ffi table [a:2 3]</pre></div>
</div>
<div class="section" id="rlwrap">
<h3 class="section">1.5 rlwrap</h3>
<p>Although you only need the <samp>k</samp> binary to run k9 most will also install rlwrap, if not already installed, in order to get command history in a terminal window. rlwrap is “Readline wrapper: adds readline support to tools that lack it” and allows one to arrow up to go through the command buffer history.
</p>
<p>In order to start k9 you should either run <samp>k</samp> or <samp>rlwrap k</samp> to get started. Here I will show both options but one should run as desired. In this document lines with input are shown with a leading space and output will be without. In the examples below the user starts a terminal window in the directory with the <samp>k</samp> binary file. Then the users enters <kbd>rlwrap ./k <span class="key">RET</span></kbd>. k9 starts and displays the date of the build, (c), and shakti and then listens to user input. In this example I have entered the command to exit k9, <kbd>\\</kbd>. Then I start k9 again without rlwrap and again exit the session.
</p>
<div class="example">
<pre class="example"> rlwrap ./k
Sep 13 2020 16GB (c) shakti
\\
./k
Sep 13 2020 16GB (c) shakti
\\
</pre></div>
</div>
<div class="section" id="REPL">
<h3 class="section">1.6 REPL</h3>
<p>k9 runs as a read, evaluation, print loop (REPL). This means that one either programs in an interactive programming environment (eg. a shell/terminal window) or by running a script. There is no reason to compile code into an executable.
</p>
</div>
<div class="section" id="Simple-example">
<h3 class="section">1.7 Simple example</h3>
<p>Here I will start up k9, perform some trivial calculations, and then close the session. After this example it will be assumed the user will have a k9 session running and working in repl mode. Comments (<code>/</code>) will be added to the end of lines as needed. One can review <a href="#plus">plus</a>, <a href="#where">where</a>, <a href="#floor">floor</a> and <a href="#timing">timing</a> as needed.
</p>
<div class="example">
<pre class="example"> 1+2 / add 1 and 2
3
!5 / generate a list of 5 integers from 0 to 4
0 1 2 3 4
1+!5 / add one to each element of the list
1 2 3 4 5
!_100e6; / generate a list of 100 million integers (suppress output with ;)
1+!_100e6; / do 100 million sums
\t 1+!_100e6 / time the operations in milliseconds
82
</pre></div>
<p>Now let’s exit the session.
</p>
<div class="example">
<pre class="example"> \\
bash-3.2$
</pre></div>
</div>
<div class="section" id="Document-formatting-for-code-examples">
<h3 class="section">1.8 Document formatting for code examples</h3>
<p>This document uses a number of examples to familiarize the reader with k9. The syntax is to have input with a leading space and output without a leading space. This follows the terminal syntax where the REPL input has space but prints output without.
</p><div class="example">
<pre class="example"> 3+2 / this is input
5 / this is output
</pre></div>
</div>
<div class="section" id="k9-idiosyncracies">
<h3 class="section">1.9 k9 idiosyncracies</h3>
<p>One will need to understand some basic rules of k9 in order to progress. These may seem strange at first but the faster you learn them, the faster you’ll move forward. Also, some of them, like overloading based on number of arguments, add a lot of expressability to the language.
</p>
<ul class="section-toc">
<li><a href="#Colon-_0028_003a_0029-is-used-to-set-a-variable-to-a-value" accesskey="1">Colon (<code>:</code>) is used to set a variable to a value</a></li>
<li><a href="#Percent-_0028_0025_0029-is-used-to-divide-numbers" accesskey="2">Percent (<code>%</code>) is used to divide numbers</a></li>
<li><a href="#Evaluation-is-done-right-to-left" accesskey="3">Evaluation is done right to left</a></li>
<li><a href="#There-is-no-arithmetic-order" accesskey="4">There is no arithmetic order</a></li>
<li><a href="#Operators-are-overloaded-depending-on-the-number-of-arguments_002e" accesskey="5">Operators are overloaded depending on the number of arguments.</a></li>
<li><a href="#Lists-and-functions-are-very-similar_002e" accesskey="6">Lists and functions are very similar.</a></li>
<li><a href="#k9-notions-of-Noun_002c-Verb_002c-and-Adverb" accesskey="7">k9 notions of Noun, Verb, and Adverb</a></li>
</ul>
<div class="subsection" id="Colon-_0028_003a_0029-is-used-to-set-a-variable-to-a-value">
<h4 class="subsection">1.9.1 Colon (<code>:</code>) is used to set a variable to a value</h4>
<p><code>a:3</code> is used to set the variable, a, to the value, 3. <code>a=3</code> is an equality test to determine if a is equal to 3.
</p>
</div>
<div class="subsection" id="Percent-_0028_0025_0029-is-used-to-divide-numbers">
<h4 class="subsection">1.9.2 Percent (<code>%</code>) is used to divide numbers</h4>
<p>Yeah, 2 divided by 5 is written as <code>2%5</code>, not <code>2/5</code>. This choice is because <code>%</code> is similar to ÷, and the \ and / symbols are used elsewhere.
</p>
</div>
<div class="subsection" id="Evaluation-is-done-right-to-left">
<h4 class="subsection">1.9.3 Evaluation is done right to left</h4>
<p>2+5*3 is 17 and 2*5+3 is 16. 2+5*3 is first evaluated on the right most portion, 5*3, and once that is computed then it proceeds with 2+15. 2*5+3 goes to 2*8 which becomes 16.
</p>
</div>
<div class="subsection" id="There-is-no-arithmetic-order">
<h4 class="subsection">1.9.4 There is no arithmetic order</h4>
<p>+ has equal precedence as *. The order of evaluation is done right to left unless parenthesis are used. (2+5)*3 = 21 as the 2+5 in parenthesis is done before being multiplied by 3.
</p>
</div>
<div class="subsection" id="Operators-are-overloaded-depending-on-the-number-of-arguments_002e">
<h4 class="subsection">1.9.5 Operators are overloaded depending on the number of arguments.</h4>
<div class="example">
<pre class="example"> *(13;6;9) / single argument: * returns the first element
13
2*(13;6;9) / two arguments: * is multiplication
26 12 18
</pre></div>
</div>
<div class="subsection" id="Lists-and-functions-are-very-similar_002e">
<h4 class="subsection">1.9.6 Lists and functions are very similar.</h4>
<p>k9 syntax encourages you to treat lists and functions in a similar function. They should both be thought of a mapping from a value to another value or from a domain to a range. Lists and functions do not have the same type.
</p>
<div class="example">
<pre class="example"> l:3 4 7 12
f:{3+x*x}
l@2
7
f@2
7
@l
`I
@f
`.
</pre></div>
</div>
<div class="subsection" id="k9-notions-of-Noun_002c-Verb_002c-and-Adverb">
<h4 class="subsection">1.9.7 k9 notions of Noun, Verb, and Adverb</h4>
<p>k9 uses an analogy with grammar to describe language syntax. The k9 grammar consists of nouns (data), verbs (functions) and adverbs (function modifiers).
</p><ul>
<li> <tt>The boy ate an appple. (Noun verb noun)</tt>
</li><li> <tt>The girl ate each olive. (Noun verb adverb noun)</tt>
</li></ul>
<p>In k9 as the Help/Info card shows data are nouns, functions/lists are verbs and modifiers are adverbs.
</p><ul>
<li> <tt>3 > 2 (Noun verb noun)</tt>
</li><li> <tt>3 >' (1 12;1 4 5) (Noun verb adverb noun)</tt>
</li></ul>
<hr>
</div>
</div>
</div>
<div class="chapter" id="Verb">
<div class="header">
<p>
Next: <a href="#Adverb" accesskey="n" rel="next">Adverbs</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">k9: Manual</a> </p>
</div>
<span id="Verbs"></span><h2 class="chapter">2 Verbs</h2>
<p>This chapter covers verbs which are the core primitives of k9. Given how different it is to call functions in k9 than in most other languages, you may have to read this chapter a few times. Once you can “speak” k9 you’ll read <code>|x</code> better than <code>reverse(x)</code>.
</p>
<p>Most functions are overloaded and change depending on the number of arguments. This can confuse new users. Eg. <code>(1 4)++(2 3;5 6;7 8)</code> contains the plus symbol once as flip and then for addition. (Remember evaluation is right to left!)
</p>
<div class="example">
<pre class="example">Verb monad
+ +
- -
* *
% <a href="#div">div</a>
! <a href="#mod">mod</a> <a href="#where">where</a>
& & <a href="#flip">flip</a>
| | <a href="#reverse">reverse</a>
< < <a href="#asc">asc</a>
> > <a href="#desc">desc</a>
= = <a href="#freq">freq</a>
~ ~ ~
, , ,
# <a href="#take">take</a> <a href="#count">count</a>
_ <a href="#drop">drop</a> <a href="#first">first</a>
^ <a href="#cut">cut</a> <a href="#sort">sort</a>
@ @ <a href="#type">type</a>
? <a href="#find">find</a> <a href="#unique">unique</a>
$ <a href="#parse">parse</a> <a href="#str">str</a>
. <a href="#dict">dict</a> <a href="#value">value</a>
</pre></div>
<span id="plus"></span><ul class="section-toc">
<li><a href="#plus-_21d2-x_002by" accesskey="1">plus ⇒ x+y</a></li>
<li><a href="#negate-_21d2-_002dx_002e" accesskey="2">negate ⇒ -x.</a></li>
<li><a href="#minus-_21d2-x_002dy_002e" accesskey="3">minus ⇒ x-y.</a></li>
<li><a href="#first-_21d2-_005fx" accesskey="4">first ⇒ _x</a></li>
<li><a href="#times-_21d2-x_002ay" accesskey="5">times ⇒ x*y</a></li>
<li><a href="#sqrt-_21d2-_0025x" accesskey="6">sqrt ⇒ %x</a></li>
<li><a href="#div-_21d2-x_0025y" accesskey="7">div ⇒ x<code>%</code>y</a></li>
<li><a href="#mod-_21d2-x_0021y" accesskey="8">mod ⇒ x!y</a></li>
<li><a href="#where-_21d2-_0021x" accesskey="9">where ⇒ !x</a></li>
<li><a href="#flip-_21d2-_0026x">flip ⇒ &x</a></li>
<li><a href="#and-_21d2-x_0026y">and ⇒ x&y</a></li>
<li><a href="#reverse-_21d2-_007cx">reverse ⇒ |x</a></li>
<li><a href="#or-_21d2-x_007cy">or ⇒ x|y</a></li>
<li><a href="#asc_0028desc_0029-_21d2-_003c-_0028_003e_0029-x">asc(desc) ⇒ < (>) x</a></li>
<li><a href="#less-_0028more_0029-_21d2-x-_003c-_0028_003e_0029-y">less (more) ⇒ x < (>) y</a></li>
<li><a href="#freq_21d2-_003dx">freq⇒ =x</a></li>
<li><a href="#equal-_21d2-x_003dy">equal ⇒ x=y</a></li>
<li><a href="#not-_21d2-_007ex">not ⇒ ~x</a></li>
<li><a href="#match-_21d2-x_007ey">match ⇒ x~y</a></li>
<li><a href="#enlist-_21d2-_002cx">enlist ⇒ ,x</a></li>
<li><a href="#cat-_21d2-x_002cy">cat ⇒ x,y</a></li>
<li><a href="#sort-_21d2-_005ex">sort ⇒ ^x</a></li>
<li><a href="#g_t_005bf_005dcut-_21d2-x_005ey">[f]cut ⇒ x^y</a></li>
<li><a href="#count-_21d2-_0023x">count ⇒ #x</a></li>
<li><a href="#g_t_005bf_005dtake-_21d2-x_0023y">[f]take ⇒ x#y</a></li>
<li><a href="#floor-_21d2-_005fx">floor ⇒ _x</a></li>
<li><a href="#g_t_005bf_005ddrop-_21d2-x_005fy">[f]drop ⇒ x_y</a></li>
<li><a href="#str-_21d2-_0024x">str ⇒ $x</a></li>
<li><a href="#parse-_21d2-x_0024y">parse ⇒ x$y</a></li>
<li><a href="#unique-_21d2-_003fx">unique ⇒ ?x</a></li>
<li><a href="#find-_21d2-x_003fy">find ⇒ x?y</a></li>
<li><a href="#type-_21d2-_0040x">type ⇒ @x</a></li>
<li><a href="#g_t_005bf_005dat-_21d2-x_0040y">[f]at ⇒ x@y</a></li>
<li><a href="#value-_21d2-_002ex">value ⇒ .x</a></li>
<li><a href="#dict-_21d2-x_002ey">dict ⇒ x.y</a></li>
</ul>
<div class="section" id="plus-_21d2-x_002by">
<h3 class="section">2.1 plus ⇒ x+y</h3>
<p>Add x and y. Arguments can be elements or lists but if both x and y are lists then they must be of equal length.
</p>
<div class="example">
<pre class="example"> 3+7
10
a:3;
a+8
11
3+4 5 6 7
7 8 9 10
3 4 5+4 5 6
7 9 11
3 4+1 2 3 / lengths don't match
!length
10:00+1 / add a minute
10:01
10:00:00+1 / add a second
10:00:01
10:00:00.000+1 / add a millisecond
10:00:00.001
</pre></div>
<span id="negate"></span></div>
<div class="section" id="negate-_21d2-_002dx_002e">
<h3 class="section">2.2 negate ⇒ -x.</h3>
<div class="example">
<pre class="example"> -3
-3
--3
3
x:4;
-x
-4
d:`a`b!((1 2 3);(4 5 6))
-d
a|-1 -2 -3
b|-4 -5 -6
</pre></div>
<span id="minus"></span></div>
<div class="section" id="minus-_21d2-x_002dy_002e">
<h3 class="section">2.3 minus ⇒ x-y.</h3>
<p>Subtract y from x.
</p>
<div class="example">
<pre class="example"> 5-2
3
x:4;y:1;
x-y
3
</pre></div>
<span id="first"></span></div>
<div class="section" id="first-_21d2-_005fx">
<h3 class="section">2.4 first ⇒ _x</h3>
<p>Return the first value of x.
</p>
<div class="example">
<pre class="example"> _1 2 3
1
_((1 2);(3 4);(5 6))
1 2
__((1 2);(3 4);(5 6))
1
_`a`b.((1 2 3);(4 5 6))
1 2 3
_|1 2 3
3
</pre></div>
<span id="times"></span></div>
<div class="section" id="times-_21d2-x_002ay">
<h3 class="section">2.5 times ⇒ x*y</h3>
<p>Mutliply x and y.
</p>
<div class="example">
<pre class="example"> 3*4
12
3*4 5 6
12 15 18
1 2 3*4 5 6
4 10 18
</pre></div>
<span id="sqrt"></span></div>
<div class="section" id="sqrt-_21d2-_0025x">
<h3 class="section">2.6 sqrt ⇒ %x</h3>
<p>Return the square root of x.
</p>
<div class="example">
<pre class="example"> %25
5.000e+00
%14.4
3.794e+00
</pre></div>
<span id="div"></span></div>
<div class="section" id="div-_21d2-x_0025y">
<h3 class="section">2.7 div ⇒ x<code>%</code>y</h3>
<p>Divide x by y.
</p>
<div class="example">
<pre class="example"> 12%5
2.400e+00
6%2
3.000e+00
</pre></div>
<span id="mod"></span></div>
<div class="section" id="mod-_21d2-x_0021y">
<h3 class="section">2.8 mod ⇒ x!y</h3>
<p>The remainder after y divided by x using integer division. x and y must be integers.
</p>
<div class="example">
<pre class="example"> 12!27
3
5!22
2
</pre></div>
<span id="where"></span></div>
<div class="section" id="where-_21d2-_0021x">
<h3 class="section">2.9 where ⇒ !x</h3>
<p>Given a list of boolean values return the non-zero indices.
</p>
<div class="example">
<pre class="example"> !00110011b
2 3 6 7
"banana"="a"
010101
!"banana"="a"
1 3 5
x@!30<x:12.7 0.1 35.6 -12.1 101.101
3.560e+01 1.011e+02
</pre></div>
<span id="flip"></span></div>
<div class="section" id="flip-_21d2-_0026x">
<h3 class="section">2.10 flip ⇒ &x</h3>
<p>Flip, or transpose, x where x is list of 2 or more dimensions.
</p>
<div class="example">
<pre class="example"> x:(1 2;3 4;5 6);x
(1 2;3 4;5 6)
&x
(1 3 5;2 4 6)
`a`b.&x
{a:1 3 5;b:2 4 6}
&`a`b.&x
[a:1 3 5;b:2 4 6]
</pre></div>
<p>Given a list of boolean values return a list of indices where the value is zero (index 0) and non-zero (index 1).
</p>
<div class="example">
<pre class="example"> &00110011b
(0 1 4 5;2 3 6 7)
"banana"="a"
010101
&"banana"="a"
(0 2 4;1 3 5)
x@&30<x:12.7 0.1 35.6 -12.1 101.101
(1.270e+01 0.100e+00 -1.210e+01;3.560e+01 1.011e+02)
</pre></div>
<p>Given a list of non-binary values, return the ascending sorted indices.
</p>
<div class="example">
<pre class="example"> & 4 1 2 -3 4 1
(,3;1 5;,2;0 4)
&`a`b`a`d
(0 2;,1;,3)
&"blue red greeen"
(4 8;,0;,7;3 6 11 12 13;,9;,1;,14;5 10;,2)
& 2023.01.01 2023.01.02 2023.01.01
(0 2;,1)
</pre></div>
<span id="min_002fand"></span></div>
<div class="section" id="and-_21d2-x_0026y">
<h3 class="section">2.11 and ⇒ x&y</h3>
<p>The smaller of x and y. One can use the over adverb to determine the min value in a list.
</p>
<div class="example">
<pre class="example"> 3&2
2
1 2 3&4 5 6
1 2 3
0 1 0 0 1 0&1 1 1 0 0 0
0 1 0 0 0 0
`a&`b
`a
&/ 3 2 10 -200 47
-200
</pre></div>
<span id="reverse"></span></div>
<div class="section" id="reverse-_21d2-_007cx">
<h3 class="section">2.12 reverse ⇒ |x</h3>
<p>Reverse the list x.
</p>
<div class="example">
<pre class="example"> |0 3 1 2
2 1 3 0
|"banana"
"ananab"
|((1 2 3);4;(5 6))
(5 6;4;1 2 3)
</pre></div>
<p>To get the last element, one can take the first element of the reverse list (*|<em class='math'>`</em>a<em class='math'>`</em>b<em class='math'>`</em>c).
</p>
<span id="max_002for"></span></div>
<div class="section" id="or-_21d2-x_007cy">
<h3 class="section">2.13 or ⇒ x|y</h3>
<p>The greater of x and y. Max of a list can be determine by use of the adverb over.
</p>
<div class="example">
<pre class="example"> 3|2
3
1 2 3|4 5 6
4 5 6
1 0 1 1 0 1|0 0 0 1 1 1
1 0 1 1 1 1
|/12 2 3 10 / use over to determine the max of a list
12
</pre></div>
<span id="asc"></span><span id="desc"></span></div>
<div class="section" id="asc_0028desc_0029-_21d2-_003c-_0028_003e_0029-x">
<h3 class="section">2.14 asc(desc) ⇒ < (>) x</h3>
<p>Sort a list or dictionary in ascending (<) or descending (>) order. Applied to a list, these will return the indices to be used to achieve the sort.
</p>
<div class="example">
<pre class="example"> <5 7 0 12
2 0 1 3
x@<x:5 7 0 12
0 5 7 12
d:`a`b`c!3 2 1;d
a|3
b|2
c|1
<d / a dictionary is sorted by value
c|1
b|2
a|3
</pre></div>
<span id="less"></span><span id="more"></span></div>
<div class="section" id="less-_0028more_0029-_21d2-x-_003c-_0028_003e_0029-y">
<h3 class="section">2.15 less (more) ⇒ x < (>) y</h3>
<p>Return true (1b) if x is less (more) than y else false (0b).
</p>
<div class="example">
<pre class="example"> 3<2
0b
2<3
1b
1 2 3<4 5 6
1 1 1
((1 2 3);4;(5 6))<((101 0 5);12;(10 0)) / size needs to match
1 0 1
1
1 0
"a"<"b"
1b
</pre></div>
<span id="freq"></span></div>
<div class="section" id="freq_21d2-_003dx">
<h3 class="section">2.16 freq⇒ =x</h3>
<p>Freq takes a list of uniform type, x, and returns a dictionary of the distinct values (key) and their number of occurences (value).
</p>
<div class="example">
<pre class="example"> ="banana"
abn.3 1 2
=0 1 0 2 10 7 0 1 12
0 1 2 7 10 12.3 2 1 1 1 1
</pre></div>
<span id="equal"></span></div>
<div class="section" id="equal-_21d2-x_003dy">
<h3 class="section">2.17 equal ⇒ x=y</h3>
<p>Return true (1b) if x is equal to y else false (0b).
</p>
<div class="example">
<pre class="example"> 2=2
1
2=3
0
2=2.
1
"banana"="abnaoo" / check strings of equal length by character
0 0 1 1 0 0
"banana"="apple" / unequal length strings error
!length
</pre></div>
<span id="not"></span></div>
<div class="section" id="not-_21d2-_007ex">
<h3 class="section">2.18 not ⇒ ~x</h3>
<p>Boolean invert of x
</p>
<div class="example">
<pre class="example"> ~1
0
~1 0 1
0 1 0
~37 0 12
0 1 0
</pre></div>
<span id="match"></span></div>
<div class="section" id="match-_21d2-x_007ey">
<h3 class="section">2.19 match ⇒ x~y</h3>
<p>Return true (1) if x matches y else false (0). A match happens if the two arguments evaluate to the same expression.
</p>
<div class="example">
<pre class="example"> 2~2
1
2~3
0
2~2.
0
"banana"~"apple"
0
`a`b~`a`b / different than = which is element-wise comparison
1
`a`b=`a`b
1 1
f:{x+y}
f~{x+y}
1
</pre></div>
<span id="enlist"></span></div>
<div class="section" id="enlist-_21d2-_002cx">
<h3 class="section">2.20 enlist ⇒ ,x</h3>
<p>Create a list from x
</p>
<div class="example">
<pre class="example"> ,3
,3
,1 2 3
1 2 3
3=,3
,1
3~,3
0
</pre></div>
<span id="cat"></span></div>
<div class="section" id="cat-_21d2-x_002cy">
<h3 class="section">2.21 cat ⇒ x,y</h3>
<p>Concatenate x and y.
</p>
<div class="example">
<pre class="example"> 3,7
3 7
"hello"," ","there"
"hello there"
C:("ab";"c";("def";"ghi"));C
ab
c
("def";"ghi")
,/C / join the list once
a
b
c
def
ghi
,//:C / converge over join until single list
"abcdefghi"
</pre></div>
<span id="sort"></span></div>
<div class="section" id="sort-_21d2-_005ex">
<h3 class="section">2.22 sort ⇒ ^x</h3>
<p>Sort list, dictionary, or table x into ascending order. Dictionaries are sorted using the keys and tables by the first column field. One can sort tables by arbitrary columns by first reordering the columns in the table using <a href="#take">take</a> or by extracting the sort column by index or expression.
</p>
<div class="example">
<pre class="example"> ^0 3 2 1
0 1 2 3
^`b`a!((0 1 2);(7 6 5)) / sort dictionary by key
[a:7 6 5;b:0 1 2]
^[[]z:`c`a`b;y:3 2 1] / sort table by 1st col
z y
- -
a 2
b 1
c 3
^`y`z#[[]z:`c`a`b;y:3 2 1] / sort table by new 1st col
y z
- -
1 b
2 a
3 c
</pre></div>