I've just been starting out in Prolog and I was hoping to perform the following task:
Make a predicate
A(P,N,L)
such that for allC
which is nth element ofL
,P(N,C)
.
Basically I want to perform a map on the range [0..N]
.
In Haskell, the language I am most familiar with, this would look like
f p n = map(p)[0..n]
(Haskell doesn't quite have predicates so I'm taking some liberties here)
or in pointfree
f = (.enumFromTo 0).map
And it seems like I should be able to do it in Prolog easily enough. Prolog's maplist/3
is basically already that so it should be a trivial modification. My definition should look something like:
A(P,N,L) :- maplist(P, ??? , L).
However I can't really figure out what to put in the blank. In Haskell I would use a function like enumFromTo
, but it seems that such a thing doesn't exist in Prolog. The closes equivalent would be between/3
, but that isn't a list so I can't use for maplist
.
Alternatively I could make my own range predicate.
The first thing I tried was:
range(0,[0]).
range(N,[N|T]) :- range(N-1,T).
A(P,N,L) :- range(N,rangeN), maplist(P, rangeN, L).
But I can't get that to resolve at all. I also tried
range(N,L):-findall(X,between(0,N,X),L),sort(L,L).
A(P,N,L) :- range(N,rangeN), maplist(P, rangeN, L).
But that just seems really clunky for such a small problem.
How might I fill in the gap in my maplist
? Am I approaching the problem in the wrong way?