Prolog for creating a custom enumeration

I am trying to create a custom enumeration using a series of facts.

greater (X, Y): - less (Y, X).

less (a, b).
less (b, c).
less (c, d).

This works great, but there is significant repetition.

I am trying to do this. Is there a way to use an array for a simple linear series of facts through conversion?

transform ([a, b, c, d])

resulting in the same less definitions.

I have already created a less definition that uses the array and the nextto / member function for testing, however I cannot add exceptions or equivalent cases, as I could, with separate declarations. Hence my interest in reducing the simple definition of a case, and then in the desire to supplement it with additional definitions.

, defmacro lisp.

.

Edit:

, assert. , . , .

set_less([]).
set_less([X,Y|L]):- assert( myless(X,Y) ) , set_less([Y|L]).
:- set_less([a,b,c,d]).

Output: 
Goal (directive) failed: user:set_less([a, b, c, d])

?- listing.

:- dynamic myless/2.

myless(a, b).
myless(b, c).
myless(c, d).

:

mylist([a,b,c,d]).

set_less([]).
set_less([_]).  ----- Key!
set_less([X,Y|L]):- assert( myless(X,Y) ) , set_less([Y|L]).
:- set_less([a,b,c,d]).

! ? , , !

+3
3
term_expansion(list_enumerate(Name,List), [mylist(Name,List)|Flist]) :-
    list_enumerate_to_list(List, Flist).

list_enumerate_to_list([], []).
list_enumerate_to_list([_], []).
list_enumerate_to_list([X,Y|Xs], [myless(X,Y)|Ys]) :-
        list_enumerate_to_list([Y|Xs], Ys).

list_enumerate(test,[a1,b1,c1,d1]).
list_enumerate(first,[a,b,c,d]).
list_enumerate(second,[f,e,g,h]).

testless(X,Y) :- myless(X,Y).
testless(X,Y) :- myless(X,Z) , testless(Z,Y).

Output---------------------------------------------------------------

?- listing.


myless(a1, b1).
myless(b1, c1).
myless(c1, d1).
myless(a, b).
myless(b, c).
myless(c, d).
myless(f, e).
myless(e, g).
myless(g, h).

?- testless(a1,c1).
true ;
false.

?- testless(X,c1).
X = b1 ;
X = a1 ;
false.

! #prolog freenode.

, .

+2

"", nextto/member , , , .

" " " ", , ?

, , univ, =.

+1

If you need a complete order of your products, you can also simply compare them with natural numbers during the consultation / compilation period. When comparing, just look at the numbers and compare them. This should be much faster if you have a lot of items.

Something like that:

% Key generation
make_keys(List, Keys) :-
    make_keys(List, 0, Keys).

make_keys([], _, []).
make_keys([El | Els], Index, [mykey(El, Index) | Ks]) :-
    NewIndex is Index + 1,
    make_keys(Els, NewIndex, Ks).

% Mapping ordering/1 to a set of mykey/2
term_expansion(ordering(List), Clauses) :-
    make_keys(List, Clauses).

% Ordering
ordering([first, second, third, fourth, fifth]).

% Comparison by looking at the keys
greater_than(X, Y) :-
    mykey(X, XK),
    mykey(Y, YK),
    XK > YK.
+1
source

Source: https://habr.com/ru/post/1728451/


All Articles