Prolog binding arguments

两盒软妹~` 提交于 2020-01-02 16:18:35

问题


In sicstus prolog, there's a predicate:

maplist(:Pred, +List)

Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written as:

maplist(pred.bind(SomeValue), List)

回答1:


maplist(P_1, Xs) will call call(P_1, X) for each element of Xs. The built-in predicate call/2 adds one further argument to P_1 and then calls this with call/1. To indicate that a further argument is needed, it is very helpful to use a name like P_1 meaning "one extra argument is needed".

So if you have already a predicate of arity 2, say, (=)/2, you will pass =(2) to maplist:

?- maplist(=(2), Xs).
Xs = [] ;
Xs = [2] ;
Xs = [2,2] ...

Since the definition in SICStus' library is unfortunately, incorrect, rather use the following definition:

:- meta_predicate(maplist(1,?)).
:- meta_predicate(maplist_i(?,1)).

maplist(P_1, Xs) :-
   maplist_i(Xs, P_1).

maplist_i([], _P_1).
maplist_i([E|Es], P_1) :-
   call(P_1, E),
   maplist_i(Es, P_1).

See this answer for more.

Just a nice further example about lists of lists.

?- Xss = [[A],[B,C]], maplist(maplist(=(E)), Xss).
Xss = [[E], [E, E]],
A = B, B = C, C = E.


来源:https://stackoverflow.com/questions/30758763/prolog-binding-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!