Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Prolog Difference lists

I've been reading about how great difference lists are and I was hoping to test some examples from the books. But it seems that you can't pass lists as input in just the same way as, for instance append([1,2,3], [4,5], X), where X=[1,2,3,4,5]. Strangely, no book I've consulted ever mentions this.

I'm running the code on swipl and I'm interested in testing out a difference append predicate:

dapp(A-B,B-C,A-C).

and a "rotate first element of list" predicate:

drotate([H|T]-T1,R-S) :- dapp(T-T1,[H|L]-L,R-S).

Any ideas, how I can test these predicates in swipl?

like image 225
Daniel Loureiro Avatar asked Jul 09 '11 14:07

Daniel Loureiro


People also ask

How do you check if two lists have the same Prolog?

Try this: same(X, X). same([A| B], [C| D]):- A=C, same(B,D). it'll return true if they are the same.

What is a difference list in Prolog?

Definition. A Difference list in Prolog is a normal list except the very end of it is a logic variable, paired with that variable. For example: [a,b,c|E]-E.

How do you check if a variable is a list in Prolog?

To check if a variable is bound to a list, you can use is_list/1 .

How are lists represented in Prolog?

In Prolog list elements are enclosed by brackets and separated by commas. Another way to represent a list is to use the head/tail notation [H|T]. Here the head of the list, H, is separated from the tail of the list, T, by a vertical bar. The tail of a list is the original list with its first element removed.


1 Answers

Try:

dapp([1,2,3|X] - X,[4,5,6] - [],Y - []).
drotate([1,2,3|X] - X,Y - []).

Y is the answer for both predicates.

like image 142
LeleDumbo Avatar answered Nov 09 '22 13:11

LeleDumbo