1

我在做一些大约 10 小时后到期的作业时遇到了一点问题。

我应该创建一个函数has-vowels?它消耗一个字符串并根据字符串是否有元音返回真或假。

示例 (has-vowels?"wow") -> true (has-vowels?"nwtfg) -> false

所以这就是我所做的,

(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”这样的混合字符串也是假的?

任何提示或提示?

谢谢!

4

4 回答 4

1

你的代码没有任何意义。首先,您必须解释基本情况。如果它是空的,它应该返回 false。

(空?元音)真)是错误的。在这里,您是说如果它是空的,它应该返回 true,这是不正确的。

同样正如 Warwick Masson 上面提到的,您只测试第一个字母。要测试其他字母,您必须对列表中的其他项目使用递归,直到您遍历所有字母。

祝你好运!

于 2013-02-27T08:00:39.387 回答
1

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

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

于 2013-02-27T04:57:32.993 回答
0

您是否仅限于使用某些特定功能?您可以将正则表达式用于此类事情。

(define vowels? 
 (lambda (list-to-evaluate)
   (if (list? (regexp-match-positions #rx"[AaEeIiOoUu]" (list->string list-to-evaluate)))
       #t
       #f
  )))

当然 (list-to-evaluate) 有形式

(#\A #\p #\p #\l #\e)

否则你可以改变

list-to-evaluate

以一个简单的字符串“Hello World”为例。

(vowels? '(#\A #\p #\p #\l #\e)) ==> true
于 2013-10-16T03:04:39.767 回答
0

这是完整的解决方案:

;; 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)
于 2013-06-23T09:42:05.050 回答