1
votes

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:

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).
1
The % character specifies a comment line (cannot have newlines), so you should add % at the start of the line 'I-index....' or put those two lines in a single one - gusbro
Also your nao_menor/1 procedure won't work as you expect. (e.g. incr(I,I) will always fail) - gusbro
I just added the % now to specify what the variables were, it's not in the original code but thank-you for that heads up. - eXistanCe
Why will the incr (I, I) allways fail? - eXistanCe
What is the meaning of incr(I,I)? - repeat

1 Answers

1
votes

Here we go:

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

Sample queries as given by the OP:

?- not_smallest(5).
true.

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