序言用单个字符替换列表中的元素列表
prolog replace list of elements from a list with a single character
我正在处理一个 prolog
问题,它是这样的,
replace all the occurrences of WordList in List with the respective characters in the CharsList to produce the NewList.
例如newList([s,e,e,y,o,u,a,t,e],[c,u,8],[s,e,e,’’,y,o,u,'',l,a,t,e,r,'',k,a,t,e], X)
将 X
绑定到 [c,'',u,'',l,8,r,'',k,8]
。
我做到的就是这个
word(_, _, [], []).
word(M, S, [M|T], [S|T1]) :- word(M, S, T, T1).
word(M, S, [H|T], [H|T1]) :- H \= M, word(M, S, T, T1).
这是做什么的
| ?- word(see, c, [see,you,later,kate], X).
X = [c,you,later,kate]
我有点不知道下一步该怎么做。谁能指点一下我该往哪个方向走?
如果我像上面那样使用替换和子列表,可以吗?
subst(_,[],_,[]).
subst(X,[X|L],A,[A|M]):-!,subst(X,L,A,M).
subst(X,[Y|L],A,[Y|M]):-subst(X,L,A,M).
sublist([X|L], [X|M]):- prefix(L,M),!.
sublist(L,[_|M]):- sublist(L,M).
prefix([],_).
prefix([X|L],[X|M]):-prefix(L,M).
嗯,我可能会这样做:
replace( [] , _ , [] ) .
replace( [Word|Words] , Map , [Replacement|Replacements] ) :-
transform( Word , Map , Replacement ) ,
replace( Words , Map , Replacements )
.
transform( X , M , Y ) :- ( member( X:Y , M ) -> true ; X=Y ) .
那应该让你说类似的话
replace( [see,you,later,kate] , [see:c,you:u,later:l8r,kate:k8], X ) .
然后回来
X = [c,u,l8r,k8] .
append/2 it's a simple library predicate made available from SWI-Prolog lists library,处理列表很方便...
newList(Ws, Cs, L, L2) :-
correspond(Ws, Cs, W, C),
append([Skip, W, Rest], L),
append([Skip, [C], Rest], L1),
!, newList(Ws, Cs, L1, L2).
newList(_, _, L, L).
correspond([X,Y,Z|_], [C|_], [X,Y,Z], C).
correspond([_,_,_|Ws], [_|Cs], W, C) :-
correspond(Ws, Cs, W, C).
此代码段任意 假设匹配 WordList 中的 3 个字符。我会让你按要求概括...
newList(Ws, Cs, L, L2) :- correspond(Ws, Cs, W, C),
append([Skip, W, Rest], L),
append([Skip, [C], Rest], L1),!,
newList(Ws, Cs, L1, L2).
newList(_, _, L, L).
correspond([X,Y,Z|_], [C|_], [X,Y,Z], C).
correspond([_,_,_|Ws], [_|Cs], W, C):- correspond(Ws, Cs, W, C).
append([], []).
append([L|Ls], As) :- join(L, Ws, As),
append(Ls, Ws).
join([],X,X).
join([X|L1],L2,[X|L3]):- join(L1,L2,L3).
即使在 ISO 中也能正常工作。
CapelliC 的代码
我正在处理一个 prolog
问题,它是这样的,
replace all the occurrences of WordList in List with the respective characters in the CharsList to produce the NewList.
例如newList([s,e,e,y,o,u,a,t,e],[c,u,8],[s,e,e,’’,y,o,u,'',l,a,t,e,r,'',k,a,t,e], X)
将 X
绑定到 [c,'',u,'',l,8,r,'',k,8]
。
我做到的就是这个
word(_, _, [], []).
word(M, S, [M|T], [S|T1]) :- word(M, S, T, T1).
word(M, S, [H|T], [H|T1]) :- H \= M, word(M, S, T, T1).
这是做什么的
| ?- word(see, c, [see,you,later,kate], X).
X = [c,you,later,kate]
我有点不知道下一步该怎么做。谁能指点一下我该往哪个方向走?
如果我像上面那样使用替换和子列表,可以吗?
subst(_,[],_,[]).
subst(X,[X|L],A,[A|M]):-!,subst(X,L,A,M).
subst(X,[Y|L],A,[Y|M]):-subst(X,L,A,M).
sublist([X|L], [X|M]):- prefix(L,M),!.
sublist(L,[_|M]):- sublist(L,M).
prefix([],_).
prefix([X|L],[X|M]):-prefix(L,M).
嗯,我可能会这样做:
replace( [] , _ , [] ) .
replace( [Word|Words] , Map , [Replacement|Replacements] ) :-
transform( Word , Map , Replacement ) ,
replace( Words , Map , Replacements )
.
transform( X , M , Y ) :- ( member( X:Y , M ) -> true ; X=Y ) .
那应该让你说类似的话
replace( [see,you,later,kate] , [see:c,you:u,later:l8r,kate:k8], X ) .
然后回来
X = [c,u,l8r,k8] .
append/2 it's a simple library predicate made available from SWI-Prolog lists library,处理列表很方便...
newList(Ws, Cs, L, L2) :-
correspond(Ws, Cs, W, C),
append([Skip, W, Rest], L),
append([Skip, [C], Rest], L1),
!, newList(Ws, Cs, L1, L2).
newList(_, _, L, L).
correspond([X,Y,Z|_], [C|_], [X,Y,Z], C).
correspond([_,_,_|Ws], [_|Cs], W, C) :-
correspond(Ws, Cs, W, C).
此代码段任意 假设匹配 WordList 中的 3 个字符。我会让你按要求概括...
newList(Ws, Cs, L, L2) :- correspond(Ws, Cs, W, C),
append([Skip, W, Rest], L),
append([Skip, [C], Rest], L1),!,
newList(Ws, Cs, L1, L2).
newList(_, _, L, L).
correspond([X,Y,Z|_], [C|_], [X,Y,Z], C).
correspond([_,_,_|Ws], [_|Cs], W, C):- correspond(Ws, Cs, W, C).
append([], []).
append([L|Ls], As) :- join(L, Ws, As),
append(Ls, Ws).
join([],X,X).
join([X|L1],L2,[X|L3]):- join(L1,L2,L3).
即使在 ISO 中也能正常工作。 CapelliC 的代码