简体   繁体   中英

Prolog Recursive Predicates

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM