1

As the title says I want to write a program that does this.

an example would be:

?- count(member(X,[1,2,3]), N). 

N = 3 

Yes

But not only for the build in member, but also for some operators like:

?- count(17 =:= 12 + 5, N). 

N = 1 

Yes

Can someone help me get started?

Cœur
  • 37,241
  • 25
  • 195
  • 267
New Guy
  • 69
  • 1
  • 1
  • 6

2 Answers2

2

Try this:

?- findall(., Goal, Ls), length(Ls, L).

Example:

?- findall(., member(X,[1,2,3]), Ls), length(Ls, L).
L = 3,
... .
mat
  • 40,498
  • 3
  • 51
  • 78
  • 3
    I again recommend you think in terms of *relations* when programming in Prolog. This question is probably related to you previous counter question, and I recommend you think this through more thoroughly. – mat Sep 21 '15 at 12:34
0

library(aggregate) has been implemented to provide solutions for your problem, and much more...

?- aggregate(count, X^member(X,[1,2,3]), N).
N = 3.

?- aggregate(count, 17 =:= 12 + 5, N).
N = 1.
CapelliC
  • 59,646
  • 5
  • 47
  • 90