2

问:构建一个两模块水星计划的简单模板是什么?Module_1 定义和导出一个简单的函数或谓词。Module_2 导入函数/谓词以计算有用的结果并输出结果。

4

2 回答 2

2

我将使用以下方法,首先使用要导出的函数或谓词或谓词定义模块(接口部分):

% File: gcd.m

:- module gcd.

:- interface.
:- import_module integer.

:- func gcd(integer, integer) = integer.

:- implementation.

:- pragma memo(gcd/2).
gcd(A, B) = (if B = integer(0) then A else gcd(B, A mod B)).

在 gcd 模块 (gcd/2) 中使用导出函数的文件:

% File: test_gcd.m

:- module test_gcd.

:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module char.
:- import_module gcd.
:- import_module integer.
:- import_module list.
:- import_module std_util.
:- import_module string.

main(!IO) :-
    command_line_arguments(Args, !IO),
    ArgToInteger = integer.det_from_string `compose` list.det_index0(Args),

    A = ArgToInteger(0),
    B = ArgToInteger(1),

    Fmt = (func(Integer) = s(integer.to_string(Integer))),
    GCD = gcd(A, B),
    io.format("gcd(%s, %s) = %s\n", list.map(Fmt, [A, B, GCD]), !IO).

在 Windows 上编译运行(cmd.exe): 请注意 mmc 也是 Windows 系统命令,所以请使用 Mercury 分发安装程序提供的 Mercury 环境:

> mmc --use-grade-subdirs -m test_gcd
> test_gcd 12 4

在 Linux/MacOS/etc(任何 Bash-like shell)上编译和运行:

$ mmc --use-grade-subdirs -m test_gcd
$ ./test_gcd 12 4
于 2014-11-10T06:56:53.913 回答
1

我阅读了 Mercury 用户指南并了解了以下内容:

$ "mmc -f module_1.m module_2.m" % 不带引号

$“mmake module_2.depend”

$“mmake模块_2”

它构建了一个可执行文件 module_2,我执行了它

$ "./module_2"

它工作正常。当所有其他方法都失败时,请阅读手册。

于 2014-11-09T03:15:11.300 回答