简体   繁体   English

Prolog:比较列表中的数字

[英]Prolog : Comparing numbers in a list

I have this block of numbers: 我有这个数字块:

num(1).
num(-2).
num(5).
num(50).
num(-3).
num(87).

I'm supposed to make a function that given a number it is supposed to check if that number is the smallest of that "list" of numbers given above. 我应该做一个给定数字的函数,应该检查该数字是否为上面给出的数字“列表”中的最小数字。

ex: 例如:

not_smallest(5).
true.

not_smallest(X).
X = 1 ;
X = -2 ;
X = 5 ;
X = 50 ;
X = 87.

What i thought was making a list with the above block of numbers , and comparing a given number to all elements of the list. 我认为是用上面的数字块制作一个列表,并将给定的数字与列表中的所有元素进行比较。 But whenever i try to load the .pl doc i get this error: 但是每当我尝试加载.pl doc时,都会出现此错误:

Syntax error: Operator expected

what i have done so far is this: 我到目前为止所做的是:

%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).

Here we go: 开始了:

not_smallest(N) :-
   num(N),
   \+ \+ (num(M), M < N).

Sample queries as given by the OP: 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.

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