% CPSC 312 2021 – Prolog Code for Lists
% Copyright David Poole 2021, Released under CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
% This file assumes that a list is either:
% empty or
% cons(H,T)
% mymember(E,L) is true if E is an element of list L
mymember(E,cons(E,_)).
mymember(E,cons(_,L)) :- mymember(E,L).
%?- mymember(X, cons(jan, cons(feb, cons(mar, empty)))).
% myappend(X,Y,Z) is true if Z is a list that contains the elememts of X followed by the elements of Y
myappend(empty,Z,Z).
myappend(cons(A,X),Y,cons(A,Z))
:- myappend(X,Y,Z).
% Some queries to try:
%?- myappend(cons(jan,cons(feb,empty)), cons(mar,empty), M3).
%?- myappend(cons(jan,cons(feb,empty)), cons(mar,empty), M3), myappend(A,B,M3).
%?- myappend(cons(jan,cons(feb,empty)), cons(mar,empty), M3), myappend(M3, cons(apr, cons(may, cons(jun, empty))), M6).
%?- myappend(cons(jan,cons(feb,empty)), cons(mar,empty), M3), myappend(M3, cons(apr, cons(may, cons(jun, empty))), M6), myappend(A,cons(may,B),M6).
%?- myappend(cons(jan,cons(feb,empty)), cons(mar,empty), M3), myappend(M3, cons(apr, cons(may, cons(jun, empty))), M6), myappend(A,cons(may,B),M6).