How to import modules in Scheme (guile especially)?
How to create a module and import it in another script in scheme? How should I compile the script when I import a module, what are the command-line arguments that has to be passed? How to import the module if it is in another directory?
Lets create a module test_module.scm with the following code in it and its location being /some/dir,
(define-module (test_module)
#: export (square
cube))
(define (square a)
(* a a))
(define (cube a)
(* a a a))
Here we have created a module using the syntax:
(define-module (name-of-the-module)
#: export (function1-to-be-exported
function2-to-be-exported))
;; rest of the code goes here for example: function1-to-be-exported
Now lets create a script that imports the module that we created named use_module.scm with this code in it, located in the current directory.
(use-modules (test_module))
(format #t "~a\n" (square 12))
Here we have used module using the syntax:
(use-modules (name-of-the-module))
;; now all the functions that were exported from the
;; module will be available here for our use
Now lets come to the compiling part, we have to set GUILE_LOAD_PATH to the location /some/dir and then compile the script.
Now lets assume that both test_module.scm and use_module.scm are in the same directory, then do this:
$ GUILE_LOAD_PATH=. guile use_module.scm
but generally do this if the module is present in /some/dir:
$ GUILE_LOAD_PATH=/some/dir guile code.scm
p.s. The easier way to do this would be to write the script that uses add-to-load-path telling guile the location of the module. Now we can compile without worrying about the environment variables.
(add-to-load-path "/some/dir")
(use-modules (name-of-the-module))
;; rest of the code