Search code examples
prologclpfdlist

Prolog Lists - Duplicate head


I am trying to make a list of 2 consecutive terms from a list of terms. So echo should return True if L1 and L2 are lists and L2 contains each element in L1 twice in a row. Without clpfd.

I want echo to take in...

echo([x,1,[b]], L).  

and return...

L = [x,x,1,1,[b],[b]].

It does work with...

echo([2,1],[2,2,1,1]).
true.

This is what I have so far...

echo([],[]).
echo([X|XS], [Y,Z|YZS]) :-
    X #= Y,
    Y #= Z,
    echo(XS, YZS).

I get back an error regarding terms =>

Domain error: `clpfd_expression' expected, found `x'

Solution

  • I guess you don't want to use CLPFD.

    Without CLPFD:

    echo([], []).
    echo([X|Xs], [X,X|Ys]):-
      echo(Xs, Ys).