K as a Prototyping Language

Dennis Shasha
Courant Institute of Mathematical Sciences
Department of Computer Science
New York University


Download K from the Kx Systems web site. This takes about one minute and includes a powerful database system.

Now that you have the software, please execute the examples as you go.

Lesson Plan

Quick Tour of K

Why is K Good?

Why is K Bad?

Unit 1: Basic Language Features

Data Types

Structuring Primitives: scalars, list/vector, dictionary

Monadic vs. Dyadic Operators

Read and Write Files

Arguments to Programs

Generate Data

Add, Subtract, Multiply, Divide, and Compare

Vectors to Scalars and to Other Vectors


Time Functions


Variance as a function

Dot Product

Matrix Multiply

n: 100
x: (n*n) _draw 10000
m1: (n;n) # x / note that n n # x won't work because n n is function app
x: (n*n) _draw 10000
m2: (n;n)  # x

dot:{[x;y] +/x*y}

r: m1 dot/:\: +m2  / dot product with the transpose.

More on Adverbs

Unit 2: Conditionals and String Manipulation



While Loops

Do Loops

Each Operator

Exercise: VP Salary Raise

VP Salary Raise, Each Approach

VP Salary Raise, Vector Approach

Group By on a Table

Recursion and Cousins

Executing Strings


Unit 3: Interprocess Communication

Mechanics of IPC

Example server.k

/ factorial
 :[n > 0

/ quadratic formula
 x: ((-b) + _sqrt((b^2) - 4*a*c)) % (2*a)
 y: ((-b) - _sqrt((b^2) - 4*a*c)) % (2*a)

/ responds to messages one at a time; 
/ buffering is automatic.
/ list[0] is the function. list[1] is the list of arguments.
 args: list[1]
 if[list[0] = `fact
 if[list[0] = `quad
	:quad[args[0]; args[1]; args[2]]

Example client.k

machine: _h	/ illustrates case where client and server
	/ run on the same machine
machine: `

ret: (machine; 1234) 4: (`fact; ,5)
ret / should be 120

ret: (machine; 1234) 4: (`quad; (1; 4; 0))
ret / should be 0 -4

Java calling K

Unit 4: Database System: KDB/KSQL

KSQL Basics -- an extended example

Performance Advantage of KSQL

KSQL and Interprocess Communication

KSQL and the Web


Start Date: