5

Is it possible to use parametrized predicate in mapList?

For example, I would like to do following: iter by list, and for each even element (list contains only numbers) map this element to some value (this value is set by parameter of predicate).

Sample queries:

?- mapList(p(red, blue), [1,2,3,4], [red, blue, red, blue]).
true.

?- mapList(p(green, blue), [1,2,3,4], [green, blue, green, blue]).
true.
repeat
  • 18,496
  • 4
  • 54
  • 166

1 Answers1

5

Yes, the predicate just get all additional parameters. After defining p/4, running your queries (once mapList has been corrected to maplist):

?- [user].
p(C1,C2,N,C) :- 0 =:= N mod 2 -> C = C2 ; C = C1.
(^D here)

?- maplist(p(red, blue), [1,2,3,4], L).
L = [red, blue, red, blue].
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 2
    @HaskellFun and more fundamentally, in Prolog, the `call` predicate (used implicitly in `maplist`) knows how to manage the arguments. So if you have `sum(X, Y, Z) :- Z is X + Y.` and you queried, `call(sum(2), 3, Z).` you would get `Z = 5`. – lurker May 26 '16 at 19:59
  • 1
    Like partial feeding functions in functional languages! –  May 26 '16 at 20:44