Functional Declarations
Declaring Functions
Users can write custom functions with the let keyword. See example below:
let three = 1 + 2
Any subsequent calls of the three function would result in the addition, and ultimately the integer value 3.
Function Parameters
Functions can have an arbitrary number of parameters (up to 255). Each parameter has a name, type, and associated real-time value. See the following example:
let sum x y = x + y
After this function declaration, sum can be called with two addable parameters. For example, If the following function was invoked, the result would be returned as 15:
sum 5 10
This instance used integers, but the sum function could easily handle floats, characters, and booleans as well.
Type Checking
Let's review the sum function again.
let sum x y = x + y
In this example, x and y can be any type that is implemented as a operand of addition. The sum function's return value can be of any type that is a valid result of addition.
Overloading Functions
Functions can always be overloaded, though there can only ever be a single unique function declaration for each parameter set. However, there can be any number of functions with a shared name, so long as the sets of their parameter types are all mutually exclusive.
See below for a practical example.
let calculate = 3;;
let calculate x = x % 3;;
let calculate x = "Hello: " ++ x
Let's review the three definitions of calculate.
- First
calculate
definition: there is no parameter given and it always results in the same value. - Second
calculate
definition: x is used in modulus, so it must be an integer. - Third
calculate
definition: x is used with concatenate operator and is added to a string. So it needs to be a String (or List).
This is a totally valid way to define multiple functionalities for a single function identifier, without signficantly losing runtime efficiency.
Local Functions
Local functions are one-scope-use function declarations.
Local functions temporarily override global function declarations. In other words, these declarations are able to be used once, and then the identifier reverts to its previous meaning.
This concept is easiest explained through example. See the declarations below:
let funct = 1;;
let funct = 2 in
funct
So what's the difference between these two expressions? Let's explore what happens during each of these.
- The first expression declares a function with no parameters named
funct
, equal to 1. - The second expression declares a local function that temporarily sets
funct
equal to 2.- It will ONLY equal 2 in the expression directly after
in
- In all subsequent expressions, it will equal 1 again.
- It will ONLY equal 2 in the expression directly after