Prolog substitution

前端 未结 5 1339
囚心锁ツ
囚心锁ツ 2021-01-22 21:32

How can I replace a list with another list that contain the variable to be replaced. for example

rep([x, d, e, z, x, z, p], [x=z, z=x, d=c], R).
R = [z, c, e, x,         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 21:41

    If you use SWI-Prolog, with module lambda.pl found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl you can write :

    :- use_module(library(lambda)).
    
    rep(L, Rep, New_L) :-
        maplist(\X^Y^(member(X=Z, Rep)
                  ->  Y = Z
                  ;   Y = X), L, New_L).
    

提交回复
热议问题