从单词返回元音 [英] Return vowels from word
本文介绍了从单词返回元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这项工作要做,我的老师给了我一个包含以下事实的序言文件:
I've got this work to do and my teacher gave me a prolog file with the following facts:
vowel(a).
vowel(e).
vowel(i).
vowel(o).
vowel(u).
consonant(b).
consonant(c).
consonant(d).
consonant(f).
consonant(g).
consonant(h).
consonant(j).
consonant(k).
consonant(l).
consonant(m).
consonant(n).
consonant(p).
consonant(q).
consonant(r).
consonant(s).
consonant(t).
consonant(v).
consonant(w).
consonant(x).
consonant(y).
consonant(z).
我需要创建一个能够返回元音的规则.我该怎么做?
And I need to create a rule that is able to return the vowels. How can I do that?
输出将是这样的:
blafoo([s,a,r,a], X).
X = [a].
我不能使用任何序言谓词.
I can't use any prolog predicate.
推荐答案
如果您同时提到 vowel/1
和 consonant/1
,您可能需要写一个纯粹的、单调的版本.毕竟,你为什么要提到consonant/1
?
If you are mentioning both vowel/1
and consonant/1
, you might be expected to write a pure, monotonic version. After all, why do you mention consonant/1
?
word_vowels([], []).
word_vowels([C|Xs], Vs) :-
consonant(C),
word_vowels(Xs, Vs).
word_vowels([V|Xs], [V|Vs]) :-
vowel(V),
word_vowels(Xs, Vs).
?- word_vowels([a,m,a,z,o,n],Vs).
Vs = [a,a,o] ;
false.
或者使用tfilter/3
:
vowel_truth(C,true) :-
vowel(C).
vowel_truth(V,false) :-
consonant(V).
?- tfilter(vowel_truth,[a,m,a,z,o,n],Vs).
Vs = [a,a,o] ;
false.
这篇关于从单词返回元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文