% deepCount(L,N). % try writing the solution first in english, declaratively % % count of an emply list is 0 % count of a non-empty list adds N to the count of the tail of the list. % N: if the head is a list then recursively count the head % N: if the head is not a list then add 1 dc([],0). dc([H|T],N) :- is_list(H), dc(H,N1), dc(T,N2), N is N1+N2. dc([H|T],N) :- not(is_list(H)), dc(T,M), N is M+1. % debag(B,S). % % an empty list no change % a list singleton no change % if the head of a list is in the tail, leave the head out. % this one finds many solutions that are sub optimal db([],[]). db([H|T],L) :- member(H,T), db(T,L). db([H|T],L) :- db(T,M), append([H],M,L). % this one uses cut debag([],[]). debag([H|T],L) :- member(H,T), !, debag(T,L). debag([H|T],L) :- debag(T,M), append([H],M,L). % sum a list of integers sum([Nth],Nth). sum( [Ith | Rest], SumAll ) :- sum(Rest,SumRest), SumAll is SumRest+Ith .