Syntax

Literals

Everything in Krill can be boiled down to these values.

LiteralSample Values
Numbers1, 1.5
Strings"hello"
Chars'a'
Boolstrue, false
Atoms:hello, :world
Unit()

The unit value is like the null value in other languages. It is often used when a lambda does not return anything.

Comments

Comments start with a hash # and extend to the rest of the line.

print "hello" # this text is ignore

Variables

You can define variables easily

x = 2

Variables are immutable

x = 2
x = 1 # Error: Variable `x` Already Bound

Blocks

Blocks are a single expression, or multiple expression inside some curlies. They cannot be used by themselves, but are used in a few places throughout the syntax.

Lambdas

There are no top level functions in krill, only lambas (which are occasionally referred to as functions in the documentation). A lambda is a set of space separated parameters followed by an arrow ->. The body of a lambda is a block. The following two lambdas are equivalent.

add1 = a b -> a + b

add2 = a b -> {
  result = a + b
  result
}

The last expression in a block is returned.

You can create a lambda that accepts no parameters by using the underscore and calling it with the unit value.

printHello = _ -> print "hello"
printHello ()
# => "hello"

Lists

Lists are as you expect

l = [1,2,3]
l[1]
# => 2

You can also creates lists with ranges that work the same way as in Haskell.

[1..10]
# => [1,2,3,4,5,6,7,8,9,10]

[1,3..10]
# => [1,3,5,7,9]

Ifs

Since everything in krill is an expression, if’s must return a value, meaning the else block must be provided.

if x == 2 then "x is two!" else {
  "x is not two."
}

For Loops

You can loop over lists with the for in syntax.

for i in [1..5] { print i }
# => 1
# => 2
# => 3
# => 4
# => 5

Operators

PrecedenceOperatorDescriptionAssociativity
1 (highest).Function compositionRight
2!, -Unary not and negate
3*, /, %Multiplication, division, modulusLeft
4++ConcatLeft
5+, -Addition and subtractionLeft
6==, !=, <=, >=, <, >Compare operators
7&&Logical ANDRight
7||Logical ORRight
9 (lowest)$Paren AvoiderRight

The function compose operator composes together two functions.

(f . g) x == f (g x)

The $ operator will evaluate the entire right side before the left. It is useful when you want to avoid parens.

f $ x + 1 = f (x + 1)