2. Using Modules
Up to this point, you have written single expressions and evaluated them on the command-line. However, sometimes you want to split your code into multiple functions or include common libraries. In order to do things like that, you will need to understand what modules are.
mod operator creates a context for converting the usage of constants into a single expression.
This module will add two arbitrary values together:
(mod (first second)
(+ first second)
And this is an example of defining a constant and a function, followed by their usage:
;;; Raises the number by one order of magnitude.
; Defines a constant value with a name.
(defconstant ORDER_OF_MAGNITUDE 10)
; Defines a function that can be called with a value.
(defun raise_magnitude (value)
(* value ORDER_OF_MAGNITUDE)
; Calls the previously defined function.
- The module takes in a
ORDER_OF_MAGNITUDEis defined as 10.
raise_magnitudefunction takes in a
valueparameter (this is different from the
valuedefined as a module parameter).
- Returns the
valuefunction parameter multiplied by the
- Calls the function with the module parameter
By now you have seen how some aspects of the language work, and we can use these concepts to write and run a simple Chialisp program. We will write a module that calculates the factorial of a number using recursion.
Put this in a file named
;;; Calculates a factorial recursively.
;;; f(n) = n * f(n - 1)
;;; f(n) = n if n <= 2
; Defines the factorial function.
(defun factorial (number)
(if (> number 1)
(* number (factorial (- number 1)))
; Calls the function with the number provided.
Run this example with the following command:
brun "$(run factorial.clsp)" "(5)"
It will compile it and run the result with a solution where
number is 5. The result of this should be the factorial of that number, which is 120. There were a few new operators used in these examples. For more information, you should refer to the operator reference. Below is a detailed explanation of how this works.
- The module takes in a
factorialfunction also takes in a
- If the number is greater than 2, returns the number times the previous factorial.
- Otherwise, returns the number itself.
- Call the recursive function with the
We can visualize this function with the input 5 as follows:
(* 5 (factorial 4))
(* 5 (* 4 (factorial 3)))
(* 5 (* 4 (* 3 (factorial 2))))
(* 5 (* 4 (* 3 (* 2 (factorial 1)))))
(* 5 (* 4 (* 3 (* 2 1))))
Which then simplifies like this:
(* 5 (* 4 (* 3 2)))
(* 5 (* 4 6))
(* 5 24)
Everything that would normally be written using iteration in an imperative language, for example array modification, is instead written using recursion in Chialisp. It can be hard to understand at first, but eventually it will make more and more sense.
Modules allow you to use functional programming in Chialisp, which makes writing it feel more natural. It also allows you to reuse your code and express it in a more clear way.
If you have any questions about modules, or anything else, remember to come ask questions on our Discord.