The longest prologue sequence

I need to remove the longest sequence of primes from a list in Prolog. I am new to Prolog and I cannot find a way to get to the longest sequence ... Here's what I have done so far:

divisible(X,Y):-
  0 is X mod Y.
divisible(X,Y):-
  X > Y + 1,
  divisible(X,Y+1).

is_prime(2).
is_prime(3).
is_prime(P):-
  integer(P),
  P>3,
  P mod 2 =\= 0,
  not(divisible(P,3)).

This removes primes from the list.

removeP([],[]).
removeP([H],[H]):-
  not(is_prime(H)).
removeP([H|T],[H|L]):-
  not(is_prime(H)),
  removeP(T,L).
removeP([H|T],L):-
  is_prime(H),
  removeP(T,L).

And here I tried to find the longest sequence, but I have no idea what to do next

longest([],[]).
longest([H],[H]):-
  is_prime(H).
longest([H],[]):-
  not(in_prime(H)).
longest([H|T],L):-
  ....
+4
source share
1 answer

especially aggregate , help to use non-determinism:

remove_longest(Pred, L, R) :-
    aggregate(max(C,Xc/Yc), P^(append([Xc,P,Yc],L), maplist(Pred,P), length(P,C)), max(C,X/Y)),
    append(X, Y, R).

the predicate (is_prime for your case) remains general. In this run example, I use only the atom identity of 'a':

?- remove_longest(=(a), [1,2,3,a,a,4,5,a], R).
R = [1, 2, 3, 4, 5, a].
0
source

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


All Articles