我正在尝试学习一点Guile Scheme,我正在gnu: Scriping Examples
查看教程目前我有以下代码:
#!/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)))
#!/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))
我使用chmod +x modules.scm
使我的主脚本可执行,然后我尝试运行脚本:./modules.scm 10 3
(应该是120),但是我收到错误:
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
所以看起来虽然我正在加载fact
,但fact.scm
过程不会被加载。我也尝试将fact.scm
重命名为fact
,如同教程所有,并更改了在shebang行之后加载脚本的参数,但结果相同。
教程错了,还是我忽略了一些简单的东西?