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?
Try this: same(X, X). same([A| B], [C| D]):- A=C, same(B,D). it'll return true if they are the same.
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.
To check if a variable is bound to a list, you can use is_list/1 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With