4

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?

Vasantha Ganesh
  • 4,570
  • 3
  • 25
  • 33
  • 1
    https://www.gnu.org/software/guile/manual/html_node/Modules.html#Modules – Michael Vehrs Jun 24 '16 at 12:53
  • I created a module test_module.scm (http://paste.lisp.org/display/319117) and a script use_module.scm (http://paste.lisp.org/display/319118). Both module and the script are in the same directory. I tried to run the script with this command "guile use_module.scm". I get this error http://paste.lisp.org/display/319119 , for all I can understand the script is not able access the module. Please help. – Vasantha Ganesh Jun 24 '16 at 16:45
  • Well I got help from the guys from the IRC and I learnt how its done. – Vasantha Ganesh Jun 24 '16 at 18:00

1 Answers1

3

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
Vasantha Ganesh
  • 4,570
  • 3
  • 25
  • 33