从字符串中删除元音(方案)
我知道这个问题的基本算法,但我无法将句子更改为条件中的列表。我创建了 make-list 是为了让自己更轻松,但我不确定将其放在代码中的何处。例如,在第一个 cond 语句中,在检查句子中的第一个元素是否是元音之前,我需要将句子作为列表。但我在语法上一直在做错误的事情。
元音-ci?如果字符是不区分大小写的元音,则返回 #t,否则返回 #f。
stenotype 接受一个句子并返回删除所有元音的句子。
(define make-list
(lambda (string)
(string->list string)))
(define stenotype
(lambda (sentence)
(cond
[(vowel-ci? (car sentence)) (stenotype (cdr sentence))]
[else (cons (car sentence) (stenotype (cdr sentence)))])))
I know the basic algorithm for this problem but I am having trouble changing the sentence into a list inside my conditional. I created make-list to make it easier on myself but I'm not sure where to put it in the code. For ex, in the first cond statement, I need the sentence to be a list before I check if the first element in the sentence is a vowel.. but I have been doing it syntactically wrong.
vowel-ci? returns #t if a character is a case insensitive vowel, and #f otherwise.
stenotype takes a sentence and returns it with all vowels removed.
(define make-list
(lambda (string)
(string->list string)))
(define stenotype
(lambda (sentence)
(cond
[(vowel-ci? (car sentence)) (stenotype (cdr sentence))]
[else (cons (car sentence) (stenotype (cdr sentence)))])))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一些不同的任务(准备输入,以便它可以由您的实现和处理本身进行处理),您已将其分解为两个不同的函数。下一步是组合这些功能,而不是重写后者以使用前者。组合函数的最简单方法是组合。组合
make-list
和stenotype
(您可能希望为这个组合命名),您就会得到您的解决方案。请注意,您在
stenotype
中缺少用于结束递归的基本情况。There are a few different tasks (preparing input so it can be processed by your implementation and the processing itself), which you've broken into two different functions. The next step is combining the functions, rather than rewriting the latter to use the former. The simplest way of combining functions is composition. Compose
make-list
andstenotype
(you may wish to name this composition) and you'll have your solution.Note that you're missing a base case in
stenotype
to end the recursion.您只需将字符串转换为列表一次,并使用
map
或递归过滤掉元音。以下过程展示了如何使用map
:这是递归的,避免了
set!
和append
:用法:
You need to convert the string to a list only once and filter out the vowels using
map
or recursion. The following procedure shows how to usemap
:This one is recursive and avoids
set!
andappend
:Usage: