如何在Prolog中包含.pl文件?

时间:2022-07-24 02:07:14

I'd like to include code from another source file. Does anyone know how to do that?

我想要包含来自其他源文件的代码。有谁知道这是怎么做到的吗?

2 个解决方案

#1


29  

If your file is called foo.pl, you can include it using

如果您的文件名为foo.pl,则可以使用它来包含它

:- [foo].

or, equivalently and a bit more explicit

或者,等效地,更明确一点

:- consult(foo).

or, if you're worried it may be loaded several times in a larger app

或者,如果您担心它可能会在较大的应用程序中多次加载

:- ensure_loaded(foo).

or, if you're using full-blown modules

或者,如果您使用的是完整的模块

:- use_module(foo).

though the exact name of the last predicate differs between Prolog versions.

虽然最后一个谓词的确切名称在Prolog版本之间有所不同。

#2


7  

If you want to include the file literally - similar to #include, use :- include('file.pl').

如果你想按字面意思包含文件 - 类似于#include,请使用: - include('file.pl')。

Most of the time it is preferable to structure your program using modules, though.

但大多数情况下,最好使用模块构建程序。

#1


29  

If your file is called foo.pl, you can include it using

如果您的文件名为foo.pl,则可以使用它来包含它

:- [foo].

or, equivalently and a bit more explicit

或者,等效地,更明确一点

:- consult(foo).

or, if you're worried it may be loaded several times in a larger app

或者,如果您担心它可能会在较大的应用程序中多次加载

:- ensure_loaded(foo).

or, if you're using full-blown modules

或者,如果您使用的是完整的模块

:- use_module(foo).

though the exact name of the last predicate differs between Prolog versions.

虽然最后一个谓词的确切名称在Prolog版本之间有所不同。

#2


7  

If you want to include the file literally - similar to #include, use :- include('file.pl').

如果你想按字面意思包含文件 - 类似于#include,请使用: - include('file.pl')。

Most of the time it is preferable to structure your program using modules, though.

但大多数情况下,最好使用模块构建程序。