Layout and calls
Most Koskari in a .ki file looks like Python: a header line ending in a
colon, then an indented body. Inside (, [, or { the reader switches
to s-expression rules, which is why a nested call is still (+ 1 2).
Two rules
What does a line mean?
A line with more than one element is a call whose first element is the
callee. A line with one element is that element. So print "hi" is
(print "hi"), and x alone is the value of x.
What does a colon mean?
A line ending in : opens a block, and the indented lines under it are
arguments to that line. def double [x]: followed by a body is one
form, (def double [x] BODY...).
Functions
Python
def double(x):
return x * 2
Koskari
def double [x]:
(* x 2)
Where is return?
The last form in the body is the result. return exists for leaving
early; Functions and scope covers
it.
Python
double = lambda x: x * 2
Koskari
def double [x] (* x 2)
Why no colon on that one?
The body fits on the header line, so there is nothing to continue. A colon is only for a body that follows on indented lines.
Conditionals
Python
def sign(n):
if n < 0:
return "negative"
elif n == 0:
return "zero"
else:
return "positive"
Koskari
def sign [n]:
if (< n 0):
"negative"
elif (== n 0):
"zero"
else:
"positive"
Why is the test in parentheses?
(< n 0) is a call to <, and a call inside a header needs its
parentheses. Only the line itself gets the implicit call.
Python
if n > 0:
print(n)
if not ready:
print("wait")
Koskari
when (> n 0):
print n
unless ready:
print "wait"
Where parentheses stay
When do I still write them?
Around a nested expression, an inner call on the line, formals and bindings, and a collection literal. The four places, each with an example, are in Koskari and s-expressions.
Run it
Save as sign.ki:
def sign [n]:
if (< n 0):
"negative"
elif (== n 0):
"zero"
else:
"positive"
@entry-point
def main []:
print (sign -3)
print (sign 0)
print (sign 9)
koskari sign.ki
negative
zero
positive
The rules, each beside the s-expression it reads to, are in Koskari and s-expressions.