从字符串中删除元音(方案)

发布于 2024-10-20 08:52:55 字数 499 浏览 1 评论 0原文

我知道这个问题的基本算法,但我无法将句子更改为条件中的列表。我创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-10-27 08:52:55

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

(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 中缺少用于结束递归的基本情况。

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

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

丶视觉 2024-10-27 08:52:55

您只需将字符串转换为列表一次,并使用 map 或递归过滤掉元音。以下过程展示了如何使用 map

(define (remove-vowels str) 
    (let ((res ()))
      (map (lambda (c) (if (not (vowel? c)) (set! res (append res (list c))))) 
           (string->list str))
      (list->string res)))

这是递归的,避免了 set!append

(define (remove-vowels str)
    (let loop ((slist (string->list str)) (res ()))
      (if (not (null? slist))
          (if (vowel-ci? (car slist))
              (loop (cdr slist) res)
              (loop (cdr slist) (cons (car slist) res)))
          (list->string (reverse res)))))

用法:

> (remove-vowels "hello, world")
"hll, wrld"
> (remove-vowels "goodbye cruel world")
"gdby crl wrld"

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 use map:

(define (remove-vowels str) 
    (let ((res ()))
      (map (lambda (c) (if (not (vowel? c)) (set! res (append res (list c))))) 
           (string->list str))
      (list->string res)))

This one is recursive and avoids set! and append:

(define (remove-vowels str)
    (let loop ((slist (string->list str)) (res ()))
      (if (not (null? slist))
          (if (vowel-ci? (car slist))
              (loop (cdr slist) res)
              (loop (cdr slist) (cons (car slist) res)))
          (list->string (reverse res)))))

Usage:

> (remove-vowels "hello, world")
"hll, wrld"
> (remove-vowels "goodbye cruel world")
"gdby crl wrld"
撩动你心 2024-10-27 08:52:55
(define make-list (string)

     (string->list string))

(cond
[(empty? make-list(sentence))empty]
      [(vowel-ci? (car make-list(sentence))) 
        (stenotype list->string ((cdr make-  list(sentence))))]
      [else (cons (car make-list(sentence)) (stenotype (cdr make-list(sentence))))])))
(define make-list (string)

     (string->list string))

(cond
[(empty? make-list(sentence))empty]
      [(vowel-ci? (car make-list(sentence))) 
        (stenotype list->string ((cdr make-  list(sentence))))]
      [else (cons (car make-list(sentence)) (stenotype (cdr make-list(sentence))))])))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文