0

I am a Prolog beginner, and I have the following length function:

mylen([H|Lc],N) :- mylen(Lc,M),N is M+1.
mylen([],0).

I would like to know if there's an easy way to query the interpreter to calculate the length of the list of evens in a list, and if not, how I would go about incrementing N only on an even number. Thanks.

Domn Werner
  • 572
  • 2
  • 10
  • 20
  • 1
    Wow. [*Deja vu*](http://stackoverflow.com/questions/28785391/just-started-on-prolog-please-show-me-how-to-counting-even-number-in-a-list). (See the link) – lurker Mar 01 '15 at 00:44
  • 1
    I voted to close as a duplicate, following @lurker's observation of a glitch in the matrix. – Shon Mar 01 '15 at 12:28

1 Answers1

1

Something similar to this is what you might be looking for

mylen([H|Lc],N) :- 
X is mod(H,2),
X == 0,
mylen(Lc,M),
N is M+1.

mylen([],0).

mylen([H|Lc],N) :-
mylen(Lc, M),
N = M.
  • Run `mylen([1,2,3,4,5], N).` How many solutions does it give? Are they all correct? How many should there be? – lurker Mar 02 '15 at 00:50