モジュールをSchemeにインポートする方法(特にガイル)?
モジュールを作成し、スキーム内の別のスクリプトにインポートする方法は? モジュールをインポートするときにスクリプトをコンパイルするにはどうすればよいですか? 渡す必要があるコマンドライン引数は何ですか? モジュールが別のディレクトリにある場合、モジュールをインポートする方法は?
次のコードを含むモジュール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
ps これを行う簡単な方法は、add-to-load-pathを使用して guile にモジュールの場所を伝えるスクリプトを作成することです。これで、環境変数を気にせずにコンパイルできます。
(add-to-load-path "/some/dir")
(use-modules (name-of-the-module))
;; rest of the code