执行prolog程序

时间:2015-01-05 10:16:01

标签: prolog swi-prolog

我有这个非常简单的prolog程序

min(P1, P2):-
(
  atom_number(P1, L1),
  atom_number(P2, L2),
  (   L1 > L2
  ->  writeln('L2 id min'),
      writeln(L2)
  ;   L1 < L2
  ->  writeln('L1 is mean'),
      writeln(L1)
  )
)

我从命令行运行它 -

swipl -f pro1.pl

尝试使用

swipl -f pro1.pro

然后在prolog终端

min(19,12).

给出错误:

ERROR: toplevel: Undefined procedure: min/2 (DWIM could not correct goal)

任何帮助&gt;

1 个答案:

答案 0 :(得分:1)

语法上,你的代码需要一些修正。假设我把x.pl这段代码放进去了

min(P1, P2):-
  atom_number(P1, L1),
  atom_number(P2, L2),
  (   L1 > L2
  ->  writeln('L2 id min'),
      writeln(L2)
  ;   L1 < L2
  ->  writeln('L1 is mean'),
      writeln(L1)
  )
.

然后我可以在shell命令行上执行

swipl x.pl

并发出一些查询

:~$ swipl x.pl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.28)
Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- min(3,5).
ERROR: atom_number/2: Type error: `atom' expected, found `3' (an integer)
?- min('3','5').
L1 is mean
3
true.

?-