I am trying to learn a bit Guile Scheme and I am looking at the tutorial at gnu: Scriping Examples
Currently I have the following code:
modules.scm:
#!/usr/bin/env sh
exec guile -l fact.scm -e '(@ (my-module) main)' -s "$0" "$@"
!#
;; Explanation:
;; -e (my-module)
;; If run as a script run the `my-module` module's `main`.
;; (Use `@@` to reference not exported procedures.)
;; -s
;; Run the script.
(define-module (my-module)
#:export (main))
;; Create a module named `fac`.
;; Export the `main` procedure as part of `fac`.
(define (n-choose-k n k)
(/ (fact n)
(* (fact k)
(fact (- n k)))))
(define (main args)
(let ((n (string->number (cadr args)))
(k (string->number (caddr args))))
(display (n-choose-k n k))
(newline)))
fact.scm
#!/usr/local/bin/guile \
-e main -s
!#
;; How to run this program?
;; Example:
;; guile -e main -s factorial-script.scm 50
;; Explanation:
;; -e specifies the procedure to run
;; -s specifies to run this as a script
;; 50 is the number we take as input to the script
(define (fact n)
(if (zero? n) 1
(* n (fact (- n 1)))))
(define (main args)
(display (fact (string->number (cadr args))))
(newline))
I make my main script executable with chmod +x modules.scm
and then I try to run the script: ./modules.scm 10 3
(should be 120), but I get an error:
Backtrace:
4 (apply-smob/1 #<catch-closure 119cb80>)
In ice-9/boot-9.scm:
705:2 3 (call-with-prompt ("prompt") #<procedure 11aa8e0 at ice-9/eval.scm:330:13 ()> #<procedure default-prom…>)
In ice-9/eval.scm:
619:8 2 (_ #(#(#<directory (guile-user) 1233140>)))
In /home/xiaolong/development/Guile/scripting/./modules.scm:
26:13 1 (main _)
18:0 0 (n-choose-k _ _)
/home/xiaolong/development/Guile/scripting/./modules.scm:18:0: In procedure n-choose-k:
In procedure module-lookup: Unbound variable: fact
So it seems that somehow the fact
procedure does not get loaded, although I am loading fact.scm
. I also tried renaming fact.scm
into fact
as the tutorial has it and changing the arguments for loading the script after the shebang line, but same result.
Is the tutorial wrong, or am I overlooking something simple?