Statements
Statements are useful when we want to cause some side effects (other than computing a value).
Side effects in Keelung include:
Output some values (or nothing)
Ask for inputs
Impose constraints on variables with assertions
Statements are Monadic
This means that the type of statements have the form of:
Applying all arguments to a statement will result in a term of type Comp result
. However, it's generally impossible to extract result
from Comp result
unless we can provide it with certain context.
Here, we will use inputList
as an example to demonstrate how to extract values from a monadic function:
We can specify how much inputs we want by supplying the length of the array, say 4
:
To extract the result (of type [t]
) and bind it to an identifier, we use do-notation:
Here, the type of result
will have type [t]
instead of Comp [t]
return
return
We can think of return
as the inverse of <-
in do-notations.
Use it at the end of our program to output some value.
It's a common practice for a statement to return Comp ()
when there's nothing to return, like void functions in C.
Last updated