0

I need help with some TURBO-Prolog Program.

There is some list, which contains only integers. The list elements should be split up into 3 lists (X, Y, Z). The X-list should contain integer values with (x mod 2 == 0 && x mod 3 == 1), the Y-list should contains integer values with (x mod 2 == 1 && x mod 3 == 0), and the Z-list should contain all other values. The values of the Z-list should be summed up.

I am trying to create a Prolog program, which looks like that:

domains
 list=integer*
predicates
 sum(integer, list)
 append(list, list, list)
 split(list, list, list, list, integer)
clauses
 append([], Z, Z).
 append([X|Y], Z, [X|U]):-append(Y, Z, U).

 sum(0, []).
 sum(X, [H|T]):-sum(U, T), X = U + H.

 split([], [], [], [], 0).
 split([H|T], X, Y, Z, Sum):- 
    H mod 2 = 0, 
    H mod 3 = 1,
    append(X, [H], _),
    split(T, X, Y, Z, Sum).
 split([H|T], X, Y, Z, Sum):- 
    H mod 2 = 0, 
    H mod 3 = 0, 
    append(Y, [H], _),
    split(T, X, Y, Z, Sum).
 split([H|T], X, Y, Z, Sum):- 
    H mod 2 = 1, 
    H mod 3 = 1, 
    append(Z, [H], _),
    split(T, X, Y, Z, Sum), 
    sum(Sum, Z).

with the query: split([4,5,6,7], X, Y, Z, Sum). I don't get a proper result (only stack overflow error or something like this). I know, that it's a very rare Prolog code version, but I really need it in that form. Any solutions how to manage my problem?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
N.Zukowski
  • 600
  • 1
  • 12
  • 31

0 Answers0