I am reading “the art of Prolog” by Sterling and Shapiro and is stuck on chapter 15.1 ‘Difference-lists’. In short the use of Xs\Ys for a difference-list does not compile as expected. I tried compiling both from swipl and emacs, and both fails.
I noticed that help(flatten).
specifically mentions difference lists, so what am I doing wrong?
I’m using: SWI-Prolog version 9.2.8 for x86_64-darwin, and sweepl (threaded, 64 bits, version 9.2.6) for emacs
I want the code to: implement difference-lists
But what I’m getting is: compile error.
?- [flatten_dl].
ERROR: /Users/neo/Library/Mobile Documents/com~apple~CloudDocs/prolog/the_art/flatten_dl.pl:20:12: Syntax error: Operator expected
ERROR: /Users/neo/Library/Mobile Documents/com~apple~CloudDocs/prolog/the_art/flatten_dl.pl:27:19: Syntax error: Operator expected
ERROR: /Users/neo/Library/Mobile Documents/com~apple~CloudDocs/prolog/the_art/flatten_dl.pl:29:20: Syntax error: Operator expected
ERROR: /Users/neo/Library/Mobile Documents/com~apple~CloudDocs/prolog/the_art/flatten_dl.pl:32:19: Syntax error: Operator expected
ERROR: /Users/neo/Library/Mobile Documents/com~apple~CloudDocs/prolog/the_art/flatten_dl.pl:35:13: Syntax error: Operator expected
true.
My code looks like this:
%program 15.2
%append_dl(As,Bs,Cs) :- The difference-list Cs is the result of appending Bs to As, where As and Bs are compatible difference-lists.
append_dl(Xs\Ys,Ys\Zs,Xs\Zs).
% program 15.2
%flatten(Xs,Ys) :-
%Ys is the flattened list containing the elements in Xs.
%@
flatten(Xs,Ys) :-
flatten_dl(Xs,Ys\[]).
flatten_dl([X|Xs],Ys\Zs) :-
flatten_dl(X,Ys\Ys1),
flatten_dl(Xs,Ys1\Zs).
flatten_dl(X,[X|Xs]\Xs) :-
constant(X),
X\=[].
flatten([],Xs\Xs).
constant(X) :- \+ var(X).