Program in prolog to verify order of a list of numbers

435 views Asked by At

I'm trying to make a program in Prolog to verify order of a list of numbers and return YES or NO.

Example: [1, 2, 3, 4] Yes

[1, 3, 4, 2] No

I get this code on the internet to see if works:

domains
    x = integer
    l = integer*

predicates
    ordered(l)

clauses
    ordered([X]).

    ordered([Head|[Head1|Tail]]) :-
        Head =< Head1,
        ordered([Head1|Tail]).

Output : 

Goal: ordered([1,2,3,4])
Yes

Goal: ordered([3,2,4,5])
No

but some errors apear in the program SWI-Prolog: [2nd line before X: "Syntax error: Operator expected"] [11 line ordered: "ordered/1: (not loaded) unreferenced"]

I really don't know what to do.

2

There are 2 answers

0
false On BEST ANSWER

Your program uses the syntax for Turbo-Prolog, PDC-Prolog, or Visual Prolog. In Prologs in the Edinburgh tradition which include SWI, and particularly ISO-Prolog, there are no sections for predicates and declarations. Instead, the entire text consists of clauses, extra declarations are given with so called directives, like :- multifile(m/3).

As an historical remark, Turbo-Prolog did not invent such sections, they where present in "Marseille-Prolog", also known as Prolog I already. (Which was the second Prolog, after Prolog 0...)

1
vmg On

As the interpreter message says: the "equal or less" operator is =<, not <=.