【问题标题】:What is the scheme syntax to import modules (guile especially)?导入模块的方案语法是什么(尤其是诡计)?
【发布时间】:2016-10-26 21:00:22
【问题描述】:

如何在 Scheme 中导入模块(尤其是诡计)?

如何创建模块并将其导入到方案中的另一个脚本中?导入模块时应该如何编译脚本,必须传递的命令行参数是什么?如果模块在其他目录,如何导入?

【问题讨论】:

标签: scheme lisp guile


【解决方案1】:

让我们创建一个模块test_module.scm,其中包含以下代码,其位置为/some/dir

(define-module (test_module)
    #: export (square
               cube))

(define (square a)
    (* a a))
(define (cube a)
    (* a a a))

这里我们使用语法创建了一个模块:

(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

现在让我们创建一个脚本来导入我们创建的名为 use_module.scm 的模块,其中包含此代码,位于当前目录中。

(use-modules (test_module))
(format #t "~a\n" (square 12))

这里我们使用了模块的语法:

(use-modules (name-of-the-module))
;; now all the functions that were exported from the 
;; module will be available here for our use

现在让我们进入编译部分,我们必须将 GUILE_LOAD_PATH 设置为位置 /some/dir 然后编译脚本。

现在假设 test_module.scm 和 use_module.scm 都在同一个目录中,然后这样做:

$ GUILE_LOAD_PATH=. guile use_module.scm

但如果模块存在于 /some/dir 中,通常会这样做:

$ GUILE_LOAD_PATH=/some/dir guile code.scm

附言更简单的方法是编写使用 add-to-load-path 告诉 guile 模块位置的脚本。现在我们可以编译,不用担心环境变量了。

(add-to-load-path "/some/dir")
(use-modules (name-of-the-module))
;; rest of the code

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多