我将使用以下方法,首先使用以下方法定义模块
您要导出的函数或谓词或谓词(接口部分):
% 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 的 shell)上编译并运行:
$ mmc --use-grade-subdirs -m test_gcd
$ ./test_gcd 12 4