Racket 使用会员的球拍列表

Racket 使用会员的球拍列表,racket,Racket,我有一个小问题,而做一些任务的工作,约10小时后到期 我应该创建一个有元音的函数?它使用字符串并根据字符串是否有元音返回true或false 示例(有元音?“哇”)->正确 (有元音?“nwtfg”->假 这就是我所做的 (define vowel-list (cons #\A (cons #\a (cons #\E (cons #\e (cons #\I

我有一个小问题,而做一些任务的工作,约10小时后到期

我应该创建一个有元音的函数?它使用字符串并根据字符串是否有元音返回true或false

示例(有元音?“哇”)->正确 (有元音?“nwtfg”->假

这就是我所做的

(define vowel-list (cons #\A 
               (cons #\a 
               (cons #\E 
               (cons #\e
               (cons #\I 
               (cons #\i
               (cons #\O 
               (cons #\o 
               (cons #\U
               (cons #\u empty)))))))))))

(define (a-vowel? vowels)
  (cond ((empty? vowels) true)
    ((member (first vowels) vowel-list) true)
    (else false )))

(define (has-vowels? word)
  (a-vowel? (string->list word)))
问题是“oio”是真的,“www”是假的,但是像“wow”这样的混合字符串也是假的

有什么提示吗


谢谢!

你没有检查单词的其余部分,只检查第一个字母。所以“oio”之所以有效是因为o是元音,“www”之所以失败是因为w不是元音,“wow”也因为w不是元音而失败


作为提示,您需要修改当列表不为空且第一个字母不是元音时发生的情况。当前您只返回false。

您的代码没有任何意义。首先,您必须解释基本大小写。如果列表为空,则应返回false

(空?元音)true)是错误的。这里你是说如果它是空的,它应该返回true,这是错误的

正如上面Warwick Masson提到的,您只测试第一个字母。要测试另一个字母,您必须对列表中的其他项目使用递归,不断迭代,直到您完成所有字母


祝你好运!

以下是完整的解决方案:

;; contains-vowel? : string -> boolean
;; checks whether a string contains a vowel.
(define (contains-vowel? a-string)
  (local (;; A list of vowels
          (define vowel-list 
           (list #\A #\a #\E #\e #\I #\i #\O #\o #\U #\u))
          ;; split-string : string -> (listof string-pieces)
          ;; converts a string into a list of string pieces.
          (define (split-string a-string)
            (string->list a-string))
          ;; has-vowel? : string-piece -> booleal
          ;; checks whether a string-piece is a vowel
          (define (has-vowel? string-piece vowels)
            (cond ((empty? vowels) false)
                  ((equal? string-piece (first vowels)) true)
                  (else (has-vowel? string-piece (rest vowels)))))
          ;; contains-vowel-list : (listof string-pieces) -> boolean
          ;; determines whether any items on a list of string-pieces
          ;; contains a piece that represents a vowel.
          (define (contains-vowel-list losp)
            (cond ((empty? losp) false)
                  ((false? (has-vowel? (first losp) vowel-list))
                   (contains-vowel-list (rest losp)))
                  (else (has-vowel? (first losp) vowel-list)))))
          (contains-vowel-list (split-string a-string))))
;; Test
(check-expect (contains-vowel? "hellk") true)
(check-expect (contains-vowel? "hhllo") true)
(check-expect (contains-vowel? "ellhh") true)
(check-expect (contains-vowel? "hhhssdd") false)
我假设您使用cons可能还不允许使用本地表达式或列表缩写。此解决方案可能更适合您的家庭作业:

;; A list of vowels
(define vowel-list (cons #\A 
           (cons #\a 
           (cons #\E 
           (cons #\e
           (cons #\I 
           (cons #\i
           (cons #\O 
           (cons #\o 
           (cons #\U
           (cons #\u empty)))))))))))
;; split-string : string -> (listof string-pieces)
;; converts a string into a list of string pieces.
(define (split-string a-string)
  (string->list a-string))
;; Test
(check-expect (split-string "ja") (cons #\j (cons #\a empty)))
;; has-vowel? : string-piece -> boolealn
;; checks whether a string-piece is a vowel
(define (has-vowel? string-piece vowels)
  (cond ((empty? vowels) false)
        ((equal? string-piece (first vowels)) true)
        (else (has-vowel? string-piece (rest vowels)))))
;; Test
(check-expect (has-vowel? #\i vowel-list) true)
(check-expect (has-vowel? #\x vowel-list) false)
;; contains-vowel-list : (listof string-pieces) -> boolean
;; determines whether any items on a list of string-pieces
;; contains a piece that represents a vowel, from a list of vowels.
(define (contains-vowel-list losp)
  (cond ((empty? losp) false)
        ((false? (has-vowel? (first losp) vowel-list))
         (contains-vowel-list (rest losp)))
        (else (has-vowel? (first losp) vowel-list))))
;; Test
(check-expect (contains-vowel-list (cons #\h (cons #\i empty))) true)
(check-expect (contains-vowel-list (cons #\h (cons #\h empty))) false)
;; contains-vowel? : string -> boolean
;; checks whether a string contains a vowel.
(define (contains-vowel? a-string)
  (contains-vowel-list (split-string a-string)))
;; Test
(check-expect (contains-vowel? "hellk") true)
(check-expect (contains-vowel? "hhllo") true)
(check-expect (contains-vowel? "ellhh") true)
(check-expect (contains-vowel? "hhhssdd") false)

您是否被限制使用某些特定功能? 您可以使用regexp来处理这类事情

(define vowels? 
 (lambda (list-to-evaluate)
   (if (list? (regexp-match-positions #rx"[AaEeIiOoUu]" (list->string list-to-evaluate)))
       #t
       #f
  )))
当然(要评估的列表) 有表格吗

(#\A #\p #\p #\l #\e)
否则你可以改变

list-to-evaluate
以一个简单的字符串“Hello World”为例

(vowels? '(#\A #\p #\p #\l #\e)) ==> true