1

我是 Prolog 的新手,正在用它来解决一个密码算术问题 CROSS+ROADS = DANGER 。但是当代码运行时,没有输出任何人都可以告诉我程序有什么问题吗?我将非常感激。

代码:

:- use_module(library(clpfd)).

cr_puzzle([C,R,O,S,S] + [R,O,A,D,S] = [D,A,N,G,E,R]) :-
    Puzzle = [ C,R ,O ,S ,A ,D, N ,G, E],
    Puzzle ins 0..9,

    all_different(Puzzle),
    labeling([],Puzzle),

    C*10000+R*1000+O*100+S*10+S+
            R*10000+O*1000+A*100+D*10+S #=
            D*100000 + A*10000+N*1000+G*100+E*10+R,

    C #\=0,R #\=0.

我正在使用 SWI-Prolog

4

2 回答 2

1

如果您将其用作运行的脚本swipl myScript.pl,则必须指定脚本的入口点,如下所示:

:-initialization(myProgEntryPoint).

% define thousand other predicates

myProgEntryPoint :- write("Do stuff"), halt.

重要的部分是:-initialization(...).最后halthalt如果要在运行脚本后进入交互式解释器,请删除。

顺便说一句:你应该修复你的缩进,否则代码很快就会变得不可读。

于 2015-04-27T15:59:45.807 回答
1

您的程序在运行时没有产生任何输出的最可能原因是它没有运行

$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.4)
Copyright (c) 1990-2015 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).

?- compile(cr).
true.

?- cr_puzzle(P).
P = ([9, 6, 2, 3, 3]+[6, 2, 5, 1, 3]=[1, 5, 8, 7, 4, 6]) ;
false.

确保您熟悉

此外,有关详细信息,请参阅 Prolog 处理器的手册。

于 2015-08-13T01:23:20.123 回答