从字符串中删除元音(方案) [英] Removing vowels from a String (Scheme)

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

问题描述

我知道这个问题的基本算法,但我无法将句子更改为条件语句中的列表.我创建了 make-list 来让自己更容易,但我不确定把它放在代码中的什么地方.例如,在第一个 cond 语句中,在检查句子中的第一个元素是否为元音之前,我需要将句子作为一个列表.但我一直在做语法错误.

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.

元音词?如果字符是不区分大小写的元音,则返回 #t,否则返回 #f.

vowel-ci? returns #t if a character is a case insensitive vowel, and #f otherwise.

stenotype 接受一个句子并返回它并删除所有元音.

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

推荐答案

有几个不同的任务(准备输入,以便它可以由您的实现和处理本身处理),您已将它们分解为两个不同的功能.下一步是组合功能,而不是重写后者以使用前者.组合函数的最简单方法是组合.组合 make-liststenotype(您可能希望为该组合命名),您将获得解决方案.

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 and stenotype (you may wish to name this composition) and you'll have your solution.

(define double
    (lambda (x) (* x 2)))

(define inc
    (lambda (x) (+ x 1)))

; one option: define a new function that's a composition    
(define double-inc
    (lambda (x) (inc (double x))))

; another option: compose the functions when you use them
(inc (double 23))

; yet another option: make the other functions local to the composition
; Useful if the other functions are subordinate to the composition, and 
; aren't useful outside of it. You often see this with recursive functions,
; where the outer function sets up a call to the recursive function
(define (double-inc x)
    (define (double x) (* x 2))
    (define (inc x) (+ x 1))
  (inc (double x)))

(define (max numbers)
    (define (max-recur maximum numbers)
      (cond ((eq? numbers '()) maximum)
            ((< maximum (car numbers)) (max-recur (car numbers) (cdr numbers)))
            (else (max-recur maximum (cdr numbers)))))
  (max-recur (car numbers) (cdr numbers)))

请注意,您在 stenotype 中缺少一个基本情况来结束递归.

Note that you're missing a base case in stenotype to end the recursion.

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

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