Standard Library

This section details all functions that are globally available in the language. Most of them are builtin to the compiler. Some are part of the stdlib that is loaded first.

The Krill standard library is data last. This means that the main data you pass to a function will always be the last argument. This allows you to more easily create complex functions by composing together smaller functions, and then passing in your data last.

print

Print the argument to standard out.

print :: a -> ()
print "Hello, World!"
# => Hello, World!

length

Get the length of a list or string.

length :: [a] -> Number
length [1,2,3] == 3
length "hello" == 5

not

The same as the logical not operator (!).

not :: Bool -> Bool
not true == false
not false == true

id

The identity function.

id :: a -> a
id 1 == 1
id :hello = :hello

const

Returns the first argument.

const :: a -> b -> a
const 1 2 == 1
const true false == true

map

Apply a function to every element of a list and return the result.

map :: (a -> b) -> [a] -> [b]
map (x -> x * x) [1,2,3]
# => [1,4,9]

foldl

Combine elements of a list by applying a function over the elements from left-to-right.

foldl :: (b -> a -> b) -> b -> [a] -> [b]
foldl (acc current -> acc - current) 0 [1,2,3]
# => -6

foldr

Combine elements of a list by applying a function over the elements from right-to-left.

foldr :: (b -> a -> b) -> b -> [a] -> [b]
foldr (acc current -> acc - current) 0 [1,2,3]
# => 2

reduce

Alias for foldl.

filter

Return new list of elements that pass a predicate.

filter :: (a -> Bool) -> [a] -> [a]
filter (x -> x % 2 == 0) [1,2,3,4,5]
# => [2,4]

sum

Sum all the elements of a list.

sum :: [Number] -> Number
sum [1,2,3]
# => 6

min

Return the min of both arguments.

min :: Number -> Number -> Number
min 1 2 == 1

max

Return the max of both arguments.

max :: Number -> Number -> Number
max 1 2 == 2

flatten

Recursively flattens a list.

flatten :: [a] -> [a]
flatten [[1,2,3], [[1,2], [1], 2], []]
# => [1,2,3,1,2,1,2]

split

Split a string on a delimiter. Splitting on the empty string breaks string into list of character.

split :: String -> String -> [String]
split " " "this is some text" = ["this", "is", "some", "text"]
split "" "hello" == ['h', 'e', 'l', 'l', 'o']

intersperse

Insert a value in between every element of a list.

intersperse :: a -> [b] -> [c]
intersperse "," ["hello", "world"] == ["hello", "," ,"world"]
intersperse 0 [1, 2, 3] == [1, 0, 2, 0, 3]

join

Concat every element of a list together separated by a delimiter. The result is always a string.

join :: a -> [b] -> String
join " " ["hello", "world"] == "hello world"
join 0 [1, 2, 3] == "10203"

trim

Remove leading and trailing whitespace.

trim :: String -> String
trim "\n  Hello, World!     " == "Hello, World!"

lines

Split input string on all newlines.

lines :: String -> [String]
lines "hello\nworld" == ["hello", "world"]

words

Split input string on all word breaks.

words :: String -> [String]
words "  hello    world" == ["hello", "world"]

charToCode

Get the ascii code of a character.

charToCode :: Char -> Number
charToCode 'a' == 97

codeToChar

Convert ascii value to a character. The input must be an integer between 0 and 127.

codeToChar :: Number -> Char
codeToChar 97 == 'a'

toNumber

Try to convert input to a number. Throws an error if it cannot.

toNumber :: a -> Bool
toNumber "3" == 3
toNumber "hello"
# => Error: Could not parse "hello" to a number

toString

Convert input to a string.

toString :: a -> String
toString [1,2] == "[1,2]"
toString true == "true"

isList

Check if input is a number

isList :: a -> Bool
isList [1,2] == true
isList 1 == false

isNumber

Check if input is a number.

isNumber :: a -> Bool
isNumber "hello" == false
isNumber 1 == true

isString

Check if input is a string.

isString :: a -> Bool
isString "hello" == true
isString 1 == false

isBool

Check if input is a bool.

isBool :: a -> Bool
isBool true == true
isBool "hello" == false

isAtom

Check if input is an atom.

isBool :: a -> Bool
isBool :hello == true
isBool "hello" == false

date

Get the current date in “year-month-day” format.

date :: _ -> String
date () == "2019-5-11"

time

Get the current time in “hour:minute” format.

time :: _ -> String
time () == "17:40"

readFile

Read a file from disk. Throw and error if file not found.

readFile :: String -> String

writeFile

Write a string to a file on disk. The file is created if it doesn’t exist.

writeFile :: String -> String -> ()

appendFile

Append to a file on disk. The file is created if it doesn’t exist.

writeFile :: String -> String -> ()

pow

Raise the first argument to the power of the second.

pow 2 3 == 8

root

Get the nth root of an argument.

root 3 8 == 2

sqrt

Get the square root of the argument.

sqrt 25 == 5

floor

Floor the input.

floor 2.6 == 2

ceil

Ceil the input.

ceil 2.2 == 3

sin

Get the sin of the input.

sin 0 == 0

cos

Get the cos of the input.

cos 0 == 1

throwError

Throw an error with input as message. This cannot be caught.

throwError "something bad!"
# => Error: something bad!

assert

Compare both inputs and throw an error if they are not equal.

assert 2 2
# => ()

assert 2 3
# => Error: 2 does not equal 3

args

Special variable that contains all arguments passed to krill. This is only non-empty when running a file.

# src/file.kr
print args
krill src/file.kr hello world
["/src/file.kr", "hello", "world"]