From ok@atlas.otago.ac.nz  Mon Nov 20 00:59:29 2000
Received: from atlas.otago.ac.nz (atlas.otago.ac.nz [139.80.32.250])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id AAA11493
	for <prolog@swi.psy.uva.nl>; Mon, 20 Nov 2000 00:59:27 +0100 (MET)
Received: (from ok@localhost)
	by atlas.otago.ac.nz (8.9.3/8.9.3) id MAA10077;
	Mon, 20 Nov 2000 12:59:22 +1300 (NZDT)
Date: Mon, 20 Nov 2000 12:59:22 +1300 (NZDT)
From: "Richard A. O'Keefe" <ok@atlas.otago.ac.nz>
Message-Id: <200011192359.MAA10077@atlas.otago.ac.nz>
To: aikguitarist@usa.net, prolog@swi.psy.uva.nl
Subject: Re:  numPositive

aikGuitarist <aikguitarist@usa.net> wrote:
	I'm writing a prolog predicate:
	
	% numPos(List, N) succeeds iff List is a list, and there are exactly N 
	% positive integers in it at the TOP LEVEl. 
	
	numPos([], 0).
	numPos([Head|Tail], N):-integer(Head), Head>0, X is N-1, numPos(Tail,X).
	numPos([Head|Tail], N):-numPos(Tail,X).
	
	And it throws a number exception error. ???
	
Well, yes, of course.  When you do X is N-1, what's N?	

PLEASE do not use the baStudlyCaps style for predicate names.
The underscore exists and lets us write readable names.  Let us use it!

Two ways.  Body recursive:	

	num_pos([], 0).
	num_pos([X|Xs], N) :-
	    num_pos(Xs, M),
	    ( integer(Head), Head > 0 -> N is M + 1 ; N is M ).

and tail recursive, using an accumulator parameter:

	num_pos(Xs, N) :-
	    num_pos(Xs, 0, N).

	num_pos([], N, N).
	num_pos([X|XS], N0, N) :-
	    ( integer(Head), Head > 0 -> N1 is N0 + 1 ; N1 is N0 ),
	    num_pos(Xs, N1, N).

Then there's the prettier version using filter and length, which I'll omit.

