Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
pos_diff(X,Y,X-Y) :- X > Y. pos_diff(X,Y,Y-X). % ?- pos_diff(2,3,Diff). % ----------------------------------------- % ?- member(X, [1,2]). % ?- member(3, [A,B]). % ?- member(55, L). % L = [55|_G1028] ; % L = [_G1027, 55|_G1031] ; % L = [_G1027, _G1030, 55, _G1034] ; ... % ----------------------------------------- % append is a built-in predicate in Prolog. % append([],L,L). % append([X|L1],L2,[X|L3]) :- append(L1,L2,L3). % Notes about second rule: % 1. The first element of the first list (X) will always be the first % element of the 3rd list. % 2. The tail of the first list (L1) will always have the second % argument (L2) appended to it to form the tail (L3) of the third % argument. % 3. We need the recursive call to do the work mentioned in 2. % 4. Since we continually remove the first element of the first list to % form the subgoal in the body, it will gradually be reduced to the % empty list. % ?- append([a,b,c],[3,2,1],[a,b,c,3,2,1]). % ?- append([a,b],[c,d],X). % ?- append(X,[b,c,d],[a,b,c,d]). % ?- append([a],X,[a,b,c]). % ?- append(L1,L2,[a,b,c]). % ?- append(L1,L2,L). % ?- append([X,55],[Y],[77,Z,20]). % ----------------------------------------- mylength([],0). mylength([_|T],N) :- mylength(T,N1), N is N1 + 1. % ?- mylength([a,b,c,3,2,1],N). % ?- mylength([[a,b,c],[3,2,1]],N). % ?- mylength([a,b|Tail],N). % ----------------------------------------- listsum([],0). listsum([X|L],Sum) :- number(X), listsum(L,S), Sum is S+X. % ?- listsum([1,2,3,4,5.5,6],S). % ?- listsum([1,2,3,a,b,c],S).