I'm new in prolog and I'm trying to write the predicate encode(L,L1)
which counts the duplicates of elements in L
,for example:
encode([4,4,4,3,3],L).
L=[3,4,2,3].
This is what I have written:
encode(L,L1) :- encode(L,1,L1).
encode([],_,[]).
encode([H],N,[N,H]).
encode([H,H|T],N1,[N,H|T1]) :- M is N1+1, encode([H|T],M,[N,H|T1]).
encode([H,Y|T],N,[N,H|T1]) :- H\=Y, encode([Y|T],T1).
The above predicate is not reversible. It only works if the first parameter is provided.
How can I write encode reversible?
For example:
encode(L,[3,4,2,3]).
L = [4,4,4,3,3].
M is N1+1
tryM #= N1 + 1
. Make sure you load the CLP(FD) module (:- use_module(library(clpfd)).
) And in place of\=
use\==
. – lurker