Remove vowels in the list

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

So far I have had the vowel(X):- member(X,[a,e,i,o,u]) condition vowel(X):- member(X,[a,e,i,o,u]) . Then I thought about what removes all elements from another 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 on the removal of those elements that should be members of [a,e,i,o,u] . Although I still don’t know anything.

+1
prolog
Nov 21 '12 at 10:12
source share
3 answers

Here is the code

 deleteV([H|T],R):-member(H,[a,e,i,o,u]),deleteV(T,R),!. deleteV([H|T],[H|R]):-deleteV(T,R),!. deleteV([],[]). 

What is he doing? First he asks a question? Vowel head Yes-> We ignore it. No-> We need it. If he finds an empty list, he creates a list of results, and when returning from backtracking he attaches the consons forward. This code has been tested by SWIProlog.

0
Nov 23
source share

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

First we define list_memberd_t/3 , which behaves the same as memberd_truth/3 but has a different order of arguments:

 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 brevity, define memberd_t/3 based on list_memberd_t/3 :

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

As a parallel with library(apply) , 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 texclude/3 above, use tinclude/3 with the auxiliary predicate not/3 to translate the true 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). 

Request example:

 ?- 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]. 
+4
Apr 30 '15 at 9:05
source share

here is a solution using DCG. Notice how the "exit" is obtained (without passing arguments, only lists of differences)

 novowels --> ("a";"e";"i";"o";"u"), !, novowels. % or .. % novowels --> [C], {memberchk(C, "aeiou")}, !, novowels. novowels, [C] --> [C], !, novowels. novowels --> []. 

I must admit that I do not like the second cut, but it seems necessary.

Test:

 ?- phrase(novowels, "abcdefghilmnopq", L),format('~s',[L]). bcdfghlmnpq L = [98, 99, 100, 102, 103, 104, 108, 109, 110|...]. 

edit About the second section, it seems that the designation of the "left hand" is required: if I encode the argument, without cutting, I get the correct parsing:

 novowels(Cs) --> ("a";"e";"i";"o";"u"), !, novowels(Cs). % novowels(Cs) --> [C], {memberchk(C, "aeiou")}, !, novowels(Cs). novowels([C|Cs]) --> [C], novowels(Cs). novowels([]) --> []. 

Test:

 ?- phrase(novowels(L), "abcdefghilmnopq"),format('~s',[L]). bcdfghlmnpq L = [98, 99, 100, 102, 103, 104, 108, 109, 110|...] ; false. 

I wonder if this is a DCG translator error or (more likely) my error ...

0
Nov 25 '12 at 10:10
source share



All Articles