The Kap Programming Language

3 months ago 14

The below examples can be run in the browser by clicking on the link. This opens the web-based Kap interpreter with the code shown in the entry field. Simply click on “Send” to run the code.

Numbers

Displaying a large power of 2

This example demonstrates the support for bigints. We simply take 2 to the power of 300:

Division of integers giving rational results

If you divide two integers, the result is a rational number. This example creates an array of numbers from 1 to 10 (1+⍳10) and divides 2 by each element in the array.

The Kap interpreter provides three different ways to display rational numbers. The display mode is chosen using the ]radmode command (interpreter commands are prefixed by the ] symbol). Try using the different arguments normal, fancy or decimal to try them.

Graphics

Draw a circle generated from complex numbers

This example creates a 2-dimensional array of complex numbers, with the horizontal axis representing real numbers between -20 and 20, and the vertical axis representing complex numbers -20i and 20i, and each vaue in the array being the result of the sum of these numbers

Then the absolute value (using the function |) is used to get the length of the vector and this value is then compared with 20 in order to get a binary array where the value is 1 where the length is greater than 20 and 0 where it’s less than 20. Drawing this array results in a rather low resolution circle on the screen.

gui:draw 20 < | { (0j1×⍵) +⌻ ⍵ } (⍳40)-20

Animated graphics

While the current implementation of graphics support in Kap is limited, one can still do basic animation with it. Press “Stop” to stop the animation.

c ← 2|⌊| (0j1×)⍛(+⌻) 15.0÷⍨(⍳400)-200 i←0 while (1) { offset0 ← 50×1+math:sin (i×0.2) (i×0.3) offset1 ← 50×1+math:sin (i×0.15) (i×0.25) gui:draw 0 , ((300 300 ↑ offset0↓c) ,[1.5] (300 300 ↑ offset1↓c)) i←i+1 }

Game of life

⍝ Define L to be a function to compute the next iteration L ⇐ {⊃ 1 ⍵ ∨∙∧ 3 4 = +/ , ¯1 0 1 ⊖⌻ ¯1 0 1 ⌽¨ ⊂⍵} ⍝ curr is a 2-dimensional array containing the current state of the board. ⍝ Initialise this array with the starting pattern curr ← 50 ⊖ 50 ⌽ 100 100 ↑ 3 3 ⍴ 0 1 0 1 1 0 0 1 1 ⍝ Define a loop that computes the next iteration, draws it, and waits for 0.1 seconds while (1) { gui:draw curr←L curr time:sleep 0.1 }

Charting

Display a line chart of a 2-dimensional array

The function ⍴ is used to reshape an array fo 10 numbers into a 2-by-5 array. This array is then passed to the function chart:line which draws a line chart. You can also experiment with using the alternative charting functions: chart:bar and chart:pie.

chart:line 2 5 ⍴ 3 2 8 4 3 10 11 14 12 14

Simulate die rolls

Roll 6 6-sided dice 10000 times and do a histogram plot of the results.

chart:bar ⍸˝ ∧ +/ 1+?10000 6 ⍴ 6

Sound

Playing sounds

The function audio:play accepts a sample in the form of an array of values between -1.0 and 1.0. The sample is played at 48 kHz.

audio:play math:sin +\ 5000 / (300+50×⍳5) ÷ (48000.0÷math:pi×2)

Games

Play 2048

Below is a simple implementation of the game 2048. Use the commands u, d, l or r for up, down, left or right respectively.

addElement ⇐ {⍵ + (⍴⍵) ⍴ 2× (⍳↑⍴q) = (?+/q) ⌷ (q←,⍵=0) / ⍳×/⍴⍵} c ⇐ { 4 ↑ { ⊃ ,/ { +/¨ ((≢⍵)⍴1 0) ⊆ ⍵}¨ (1,2≠/)⍛⊆ ⍵ } 0,⍨ ⍵~0 }⍤1 h ← map @l λc @r λ(c⍢⌽) @u λ(c⍢(⊖⍉)) @d λ(c⍢(⌽⍉)) board ← 4 4 ⍴ 0 while (~2048∊board) { io:print o3:format board←addElement board dir ← h[↑io:readLine "Direction (u/d/l/r):"] board ← ⍞dir board }
Read Entire Article