K as a Prototyping Language


Dennis Shasha
Courant Institute of Mathematical Sciences
Department of Computer Science
New York University
shasha@cs.nyu.edu
http://cs.nyu.edu/cs/faculty/shasha/index.html




Download

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




Debugging




Time Functions



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





Topics




Conditionals




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




Examples



Unit 3: Interprocess Communication





Mechanics of IPC




Example server.k


/ factorial
fact:{[n]
 :[n > 0
	:n*fact[n-1]
	:1]
}

/ quadratic formula
quad:{[a;b;c]
 x: ((-b) + _sqrt((b^2) - 4*a*c)) % (2*a)
 y: ((-b) - _sqrt((b^2) - 4*a*c)) % (2*a)
 :(x;y)
}

/ responds to messages one at a time; 
/ buffering is automatic.
/ list[0] is the function. list[1] is the list of arguments.
.m.g:{[list]
 args: list[1]
 if[list[0] = `fact
	:fact[args[0]]
 ]
 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



Demo


Stock:
Start Date:


Conclusion