Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Even sized list in Prolog

Tags:

list

prolog

I've been trying to write a predicate which would evaluate the size of a list to be even or not and this has to be done without computing the length of the list or any arithmetic operations. It's supposedly easier than computing the length but I'm having trouble thinking of how to do it without that. I'm guessing a sort of recursive technique but if anyone is able to help it would be great.

like image 688
AuthenticReplica Avatar asked Nov 20 '25 17:11

AuthenticReplica


1 Answers

Yes, you want recursion. The base cases would be the smallest odd/even lists you can have, and then all you need is to figure out how to construct the recursive call so that it will boil down to the base case. You could start out by imagining a list of length 3 that's supposed to return true for "oddList". If it's not the base case, what's the next logical step? How does an odd list differ from an even one?

like image 70
keyser Avatar answered Nov 23 '25 13:11

keyser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!