0
votes

Give recursive definition in Prolog: Define a predicate that holds of an argument X if and only if X is a list and the length of X is odd.

I have been trying to solve this for ages. I am just wanting to learn ProLog for myself and found this problem in a book.

I have tried this but it probably only works for lists of even length.

mult2_length( [] ).
mult2_length( [ _, _ | Xs ] ) :-
  mult2_length( Xs ).

Can anyone please help me?

1
Can you post something you have tried that doesn't work? You will get more help more quickly if you do.Nick
@Nick I have edited it to show what I have doneuser9714805

1 Answers

1
votes

You have to have predicates like these:

list([]) :- fail.
list([_]).
list([_,_|T]) :- list(T).

You just keep removing 2 elements from list until there are 0 or 1.