I just learned prolog for a week and I have to write a prolog program that counts the number of times an element appearing in a list L at even positions.
I have try to figure out the problem for the from am 12:00 to now (am 4:00 last 16 hours) and fail.
1.What I have tried until now is like
count(_,[],0).
count(E,[E|L],C):-!,count(E,L,C1),C is C1+1.
count(E,[A|L],C):-atom(A),count(E,L,C),!.
count(E,[A|L],C):-count(E,A,C1),count(E,L,C2),C is C1+C2.
It just can count the occurence of the whole list within the element is a list.
2.The second is takeoutsecond occurence element in list
What I have tried until now is like but fail
takeout(A,[A|B],B).
takeout(A,[B|C],[B|D]) :- takeout(A,C,D).
takeoutSecond(A,[B|C],[B|D]):- takeoutSecond(A,C,D).
takeoutSecond(A,[A|B],[A|C]):- takeout(A,B,C).
3.Can you give me some hint if takeoutsecondlast without reverse? I will appreciate it!
Can you give me some hint, I do my all effort on it.
I will reply not now because I stay up late, and I apology for it.
Thank for reading my question.
you miss a very simple use of Prolog pattern matching: namely, to match the second element just ignore the first
You can use that as ‘building block’ for your assignment, but it’s so simple that really you’ll be better to integrate it in your efforts. Use that pattern in the head of your predicates.