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 the argument to standard out.
print :: a -> ()
print "Hello, World!"
# => Hello, World!
Get the length of a list or string.
length :: [a] -> Number
length [1,2,3] == 3
length "hello" == 5
The same as the logical not operator (!
).
not :: Bool -> Bool
not true == false
not false == true
The identity function.
id :: a -> a
id 1 == 1
id :hello = :hello
Returns the first argument.
const :: a -> b -> a
const 1 2 == 1
const true false == true
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]
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
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
Alias for foldl
.
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 all the elements of a list.
sum :: [Number] -> Number
sum [1,2,3]
# => 6
Return the min of both arguments.
min :: Number -> Number -> Number
min 1 2 == 1
Return the max of both arguments.
max :: Number -> Number -> Number
max 1 2 == 2
Recursively flattens a list.
flatten :: [a] -> [a]
flatten [[1,2,3], [[1,2], [1], 2], []]
# => [1,2,3,1,2,1,2]
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']
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]
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"
Remove leading and trailing whitespace.
trim :: String -> String
trim "\n Hello, World! " == "Hello, World!"
Split input string on all newlines.
lines :: String -> [String]
lines "hello\nworld" == ["hello", "world"]
Split input string on all word breaks.
words :: String -> [String]
words " hello world" == ["hello", "world"]
Get the ascii code of a character.
charToCode :: Char -> Number
charToCode 'a' == 97
Convert ascii value to a character. The input must be an integer between 0 and 127.
codeToChar :: Number -> Char
codeToChar 97 == 'a'
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
Convert input to a string.
toString :: a -> String
toString [1,2] == "[1,2]"
toString true == "true"
Check if input is a number
isList :: a -> Bool
isList [1,2] == true
isList 1 == false
Check if input is a number.
isNumber :: a -> Bool
isNumber "hello" == false
isNumber 1 == true
Check if input is a string.
isString :: a -> Bool
isString "hello" == true
isString 1 == false
Check if input is a bool.
isBool :: a -> Bool
isBool true == true
isBool "hello" == false
Check if input is an atom.
isBool :: a -> Bool
isBool :hello == true
isBool "hello" == false
Get the current date in “year-month-day” format.
date :: _ -> String
date () == "2019-5-11"
Get the current time in “hour:minute” format.
time :: _ -> String
time () == "17:40"
Read a file from disk. Throw and error if file not found.
readFile :: String -> String
Write a string to a file on disk. The file is created if it doesn’t exist.
writeFile :: String -> String -> ()
Append to a file on disk. The file is created if it doesn’t exist.
writeFile :: String -> String -> ()
Raise the first argument to the power of the second.
pow 2 3 == 8
Get the nth root of an argument.
root 3 8 == 2
Get the square root of the argument.
sqrt 25 == 5
Floor the input.
floor 2.6 == 2
Ceil the input.
ceil 2.2 == 3
Get the sin of the input.
sin 0 == 0
Get the cos of the input.
cos 0 == 1
Throw an error with input as message. This cannot be caught.
throwError "something bad!"
# => Error: something bad!
Compare both inputs and throw an error if they are not equal.
assert 2 2
# => ()
assert 2 3
# => Error: 2 does not equal 3
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"]