Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

prolog - Delete vowels in a list

Write a program that deletes vowels (String, NoVowelsString) that deletes all vowels from a given string.

So far I've got the condition vowel(X):- member(X,[a,e,i,o,u]). Then I thought of the one that deletes all the elements from the other list:

delete2([],L1,L1).
delete2([H|T],L1,L3) :-
   delete2(H,L1,R2),
   delete2(T,R2,L3).

So having these two I thought that I could put a condition to those elements being deleted that they have to be a member of [a,e,i,o,u]. Though I still haven't got anywhere.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following is based on the reification of term equality/inequality.

First, we first define list_memberd_t/3, which behaves just like the memberd_truth/3 but has a different argument order:

list_memberd_t([]    ,_,false).
list_memberd_t([Y|Ys],X,Truth) :-
   if_(X=Y, Truth=true, list_memberd_t(Ys,X,Truth)).

list_memberd_truth(Xs,X,Truth) :- list_memberd_t(Xs,X,Truth).

For the sake of brevity, let's define memberd_t/3 based on list_memberd_t/3:

memberd_t(X,Xs,Truth) :- list_memberd_t(Xs,X,Truth).

As a parallel to library(apply), let's define tinclude/3:

:- meta_predicate tinclude(2,?,?).
tinclude(P_2,Xs,Zs) :- 
    list_tinclude_list(Xs,P_2,Zs).

list_tinclude_list([],   _P_2,[]).
list_tinclude_list([E|Es],P_2,Fs0) :-
    if_(call(P_2,E), Fs0 = [E|Fs], Fs0 = Fs),
    list_tinclude_list(Es,P_2,Fs).

tfilter/3 is another name for tinclude/3:

tfilter(P_2,As,Bs) :-
   tinclude(P_2,As,Bs).

Next, we define the meta-predicate texclude/3, the opposite of tinclude/3:

:- meta_predicate texclude(2,?,?).
texclude(P_2,Xs,Zs) :- 
    list_texclude_list(Xs,P_2,Zs).

list_texclude_list([],_,[]).
list_texclude_list([E|Es],P_2,Fs0) :-
    if_(call(P_2,E), Fs0 = Fs, Fs0 = [E|Fs]),
    list_texclude_list(Es,P_2,Fs).

Now let's use them together!

?- texclude(list_memberd_truth([a,e,i,o,u]),
            [d,e,l,e,t,e,' ',v,o,w,e,l,s,' ',i,n,' ',a,' ',l,i,s,t], Filtered).
Filtered  = [d,  l,  t,  ' ',v,  w,  l,s,' ',  n,' ',  ' ',l,  s,t].

Edit

As an alternative to using above texclude/3, let's use tinclude/3 with an auxiliary predicate not/3 to flip the truth value:

:- meta_predicate not(2,?,?).
not(P_2,X,Truth) :-
   call(P_2,X,Truth0),
   truth_flipped(Truth0,Truth).

truth_flipped(true,false).
truth_flipped(false,true).

Sample query:

?- tinclude(not(list_memberd_truth([a,e,i,o,u])),
            [d,e,l,e,t,e,' ',v,o,w,e,l,s,' ',i,n,' ',a,' ',l,i,s,t], Filtered).
Filtered  = [d,  l,  t,  ' ',v,  w,  l,s,' ',  n,' ',  ' ',l,  s,t].

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...