[英]Prolog : Comparing numbers in a list
我有这个数字块:
num(1).
num(-2).
num(5).
num(50).
num(-3).
num(87).
我应该做一个给定数字的函数,应该检查该数字是否为上面给出的数字“列表”中的最小数字。
例如:
not_smallest(5).
true.
not_smallest(X).
X = 1 ;
X = -2 ;
X = 5 ;
X = 50 ;
X = 87.
我认为是用上面的数字块制作一个列表,并将给定的数字与列表中的所有元素进行比较。 但是每当我尝试加载.pl doc时,都会出现此错误:
Syntax error: Operator expected
我到目前为止所做的是:
%increments the index of a List
incr(X, X1) :-
X1 is X + 1.
%L-list containing "list" of numbers, N - elements of that "list",
I-index , C-number X is going to be compared to, X- number to compare.
nao_menor(X) :-
findall(N, num(N), L),
num(X),
I is 0,
nth0(I, L, C),
X =< C,
incr(I,I).
开始了:
not_smallest(N) :-
num(N),
\+ \+ (num(M), M < N).
OP给出的示例查询:
?- not_smallest(5).
true.
?- not_smallest(X).
X = 1
; X = -2
; X = 5
; X = 50
; X = 87.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.