1
votes

I'd like to write one prolog statement to remove every even order element from a list

input: [a,b,c,d,e] output: [a,c,e]

Can anyone help me out how can I write prolog statement for this?

1

1 Answers

2
votes

here is my solution for your problem.

remove_even_order([],[]). %this line says if I somehow get to an empty list the result I should return is an empty list 
remove_even_order([Head_ODD|[]],[Head_ODD]). 
remove_even_order([Head_ODD,Head_EVEN|Tail],[Head_ODD|RezultTail]) :- remove_even_order(Tail,RezultTail). 

The problem is a trivial one, but if you want me to I'll comment each line for you.

Here are the comments you requested:

First Line: This line says if I somehow get to an empty list the result I should return is an empty list

Second Line: Here we say if a list consists of one element only (Head_ODD) then the resulting list should consist of that head only aswell

Third Line: If our list has 2 or more elements(Head_ODD,HEAD_EVEN + Tail) in the result list i should place only the HEAD_ODD , and try to solve the rest of the Tail recursivly