问:构建两个模块的汞程序的简单模板是什么?Module_1定义并导出一个简单的函数或谓词。Module_2导入函数/谓词来计算有用的结果并输出结果。
发布于 2014-11-10 06:56:53
我将使用以下方法,首先使用要导出的函数或谓词(接口部分)定义模块:
% 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系统命令,因此请使用水星发行版安装程序提供的汞环境:
> mmc --use-grade-subdirs -m test_gcd
> test_gcd 12 4要在Linux/MacOS/etc (任何类似Bash的shell)上编译和运行:
$ mmc --use-grade-subdirs -m test_gcd
$ ./test_gcd 12 4发布于 2014-11-09 03:15:11
我阅读了“水星用户指南”并了解了以下内容:
$ "mmc -f module_1.m module_2.m“%没有引号
$ "mmake module_2.depend“
$ "mmake module_2“
它构建了一个可执行文件module_2,我执行了它。
$“./模块2”
而且运作正常。当其他一切都失败时,请阅读手册。
https://stackoverflow.com/questions/26825338
复制相似问题