简体   繁体   English

为什么 PROLOG 中的这段代码使用约束会给出运算符预期的语法错误?

[英]Why is this code in PROLOG using constraints gives Operator expected syntax error?

col_tri(Vars):- Vars=[X1,X2,X3],
                Vars in 1..3,
                X1#\=X2,
                X1#\=X3,
                X2#\=X3,
                label(Vars).

This code is giving me this error in line 2 (Vars in 1..3,): ERROR: c:/users/xxxx/desktop/prolog/tp2.pl:2:20: Syntax error: Operator expected这段代码在第 2 行(Vars in 1..3,)中给出了这个错误:错误:c:/users/xxxx/desktop/prolog/tp2.pl:2:20: Syntax error: Operator expected

Operators are things like the + in 1 + 1 , and in your code the in in Vars in 1..3 .运算符类似于 1 + 1 + 1中的 +,在您的代码in Vars in 1..3的 in。

Prolog code can define new operators at runtime. Prolog 代码可以在运行时定义新的运算符。

The in operator is not a standard part of Prolog, it's defined by the CLPFD library, which SWI Prolog has, but does not load automatically. in运算符不是 Prolog 的标准部分,它由 CLPFD 库定义,SWI Prolog 具有该库,但不会自动加载。

And in is for a single variable on the left, there is also ins for a list of variables like your Vars . in用于左侧的单个变量,还有ins用于变量列表,例如您的Vars So the code should become:所以代码应该变成:

:- use_module(library(clpfd)).

col_tri(Vars):- Vars=[X1,X2,X3],
                Vars ins 1..3,
                X1#\=X2,
                X1#\=X3,
                X2#\=X3,
                label(Vars).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM