Everything in Krill can be boiled down to these values.
Literal | Sample Values |
---|---|
Numbers | 1 , 1.5 |
Strings | "hello" |
Chars | 'a' |
Bools | true , 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 start with a hash #
and extend to the rest of the line.
print "hello" # this text is ignore
You can define variables easily
x = 2
Variables are immutable
x = 2
x = 1 # Error: Variable `x` Already Bound
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.
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 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]
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."
}
You can loop over lists with the for in syntax.
for i in [1..5] { print i }
# => 1
# => 2
# => 3
# => 4
# => 5
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 (highest) | . | Function composition | Right |
2 | ! , - | Unary not and negate | |
3 | * , / , % | Multiplication, division, modulus | Left |
4 | ++ | Concat | Left |
5 | + , - | Addition and subtraction | Left |
6 | == , != , <= , >= , < , > | Compare operators | |
7 | && | Logical AND | Right |
7 | || | Logical OR | Right |
9 (lowest) | $ | Paren Avoider | Right |
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)