1. numbers and lists
kiki works with numbers. Type 27 below and press run (or hit Enter):
Put spaces between numbers and you get a list:
The # operator counts how many items are in a list. Operators go before their argument:
Try changing the list -- add or remove numbers and run it again.
2. arithmetic
Arithmetic operators take a : prefix, the colon means "use this with two values":
The full set: :+ add, :- subtract, :* multiply, :% divide:
Arithmetic works on whole lists too -- it broadcasts across every element:
3. right to left
kiki reads right to left. The rightmost operation runs first, and its result flows left. Try this:
You got 8, not 7. kiki computed 3 :+ 1 first, then 2 :* 4. Think of data flowing from right to left, through each operation in turn.
Parentheses override the direction when you need them:
This weird at first. Remember, data enters on the right, each operation transforms it, the final result lands on the left.
4. naming things
Bind a name to a value with name: expr. Names stick around for the whole session:
Now use it:
kiki has several built-in named helpers. They work just like operators. Write the name, then the argument:
When a cell has multiple lines, only the last result is shown. Run them one at a time if you want to see each output.
One spacing rule: if the right-hand side of an assignment begins with a verb, put a space after the colon. n: #!9 assigns the count of iota 9; n:#!9 is parsed as dyadic take (n :# !9), not assignment.
5. a few useful operators
! generates a range from 0 up to (but not including) n, this is handy for making sequences:
| reverses. ^ sorts ascending. ? removes duplicates:
One-argument (monadic) operators chain naturally. To sort descending, sort then reverse:
Reminder: right to left, so ^ runs first, then |.
6. adverbs are operators that modify other operators
An adverb comes right after an operator and changes what it does. There are four: / \ ' "
/ is fold. It reduces a list to one value by applying the operator between every element. +/ is sum:
Any operator works. */ is product. |/ is maximum:
\ is scan, like fold, but it keeps every intermediate result. This gives you running totals, running maximums:
' is map, it applies an operator to every element individually:
Map is especially powerful with functions.
" is each-prior, it applies an operator between each consecutive pair. Great for finding changes between values:
7. filtering
Comparison operators produce a list of 0s and 1s:
& takes a list of 0s and 1s and returns the positions of the 1s:
:@ indexes into a list by position:
Now chain all three together to filter a list! Keep only elements that satisfy a condition:
Reading right to left: x :> 4 makes the 0/1 mask -> & finds positions of 1s -> x :@ picks those elements.
8. functions
Write a function with {...}. Inside, x is the input:
Use ' to map a function over every element in a list:
And combine with fold to do something useful, like sum of squares:
Functions can also act as predicates. Define one and use it with & and :@ to filter:
x here is the list from section 7, names persist through the whole session.
putting it all together
Here's ten days of step counts (in thousands). Poke around and try the helpers and operators you've learned:
Can you find the day numbers (positions) where steps were above average? Give it a try:
Reading right to left: mean steps computes the average -> steps :> makes a 0/1 mask -> & returns the positions of the above-average days.
And to see the actual step counts on those days:
From here, the reference has the full picture, and the idioms guide shows common patterns for sorting, grouping, normalization, and more.