删除列表中的元音 [英] Delete vowels in a list

查看:42
本文介绍了删除列表中的元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个删除元音(StringNoVowelsString)的程序,从给定的字符串中删除所有元音.

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

到目前为止,我已经得到了条件 vowel(X):- member(X,[a,e,i,o,u]).然后我想到了从另一个列表中删除所有元素的那个:

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).

所以有了这两个,我想我可以为那些被删除的元素设置一个条件,即它们必须是 [a,e,i,o,u] 的成员.虽然我还没有到任何地方.

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.

推荐答案

代码如下

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

它有什么作用?首先它质疑自己?它的头部是元音是->我们忽略它.不->我们需要它.如果它找到一个空列表,则构建结果列表,并在从回溯返回时将辅音附加在前面.此代码在 SWIProlog 中进行了测试.

What it does? First it question itself?It's the head a vowel Yes->We ignore it. No->We need it. If it finds an empty list, it constructs the result list, and when returning from backtracking it appends the consonats in front. This code was tested in SWIProlog.

这篇关于删除列表中的元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆