views:

84

answers:

1
+1  Q: 

Prolog Beginner

I'm trying to make predicates such that for lists X and Y, rmlast is true if X and Y are the same list except for the fact that Y has 1 element more. So [1] [1,2] or [3,2] [3,2,5].

rmlast([], [_]). :- true.
rmlast([X|xtail], [Y|ytail]) :- rmlast(xtail,ytail), X is Y.

This however, produces a false result for anything other than the base case.

+5  A: 

Your code doesn't work because xtail and ytail are written with a lower case letter, and are hence not variables. This works:

rmlast([], [_]).
rmlast([X|T1], [X|T2]) :- rmlast(T1, T2).

Other than that:

  1. As you see, there is no need to have variables X and Y in this case: just use X twice, in the appropriate places.

  2. You write:

    rmlast([], [_]). :- true.
    

    That is the same as:

    rmlast([], [_]).
    :- true.
    

    As you can see, that second statement adds nothing and can hence safely be removed.

Stephan202
I've never actually seen case sensitivity in variable instantiation before. That sure did fix it, thank you.
meunierd