Skip to main content

Functions

Defining functions

Function name should star with a lowercase letter

sum a b = a + b

k# tries to infer the types of the function. You can also define the function types using type declarations:

sum :: Num -> Num -> Num
sum a b = a + b

Currying

k# allows currying functions

-- sum2 :: Num a -> Num a -> Num a
sum2 = sum 2

Compositing functions

The functions should receive one argument only.

sum2 = sum 2
sum3 = sum 3
sum5 = sum2 . sum3 --read as apply sum2 after sum3
x = sum5 3 -- x is 8

Operators

note

For operator precedence look here

All operators are infix functions.

1 + 2

To use the operator as prefix function wrap the operator inside parenthesis

(+) 1 2

Function precedence

function has less precedence than operators.

abs -3 * 4 

is the same as

(double 3) * 5

Native functions

Native functions are defined by the target language. To define a native function in k# just use the native modifier with the function name and arguments but without expression

native sum a b

Function overloading

Functions in k# don't support overloading. So you can have two functions with the same name and same arity.

Lambdas

Lambdas functions allows closure over the environment.

fun sumX x = \y -> x + y

Lambda without parameters

fun double x = \-> x * 2
warning

The first argument in a lambda mustn't have an space about the lambda operator (\). In case of a lambda without arguments the lambda operator changes to (\->)