2013-02-27 49 views
1

我在约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”是假的,但混合的字符串,如“哇”也是假的?

任何提示或建议?

感谢!

回答

1

你没有检查剩下的单词,只有第一个字母。所以“oio”的作品,因为o是一个元音,“www”失败,因为w不是和“哇”也失败,因为w不是元音。

作为一个提示,你需要修改当列表不是空的时候,第一个字母不是元音。目前你只是返回false。

1

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

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

同样如上所述,华威马松你只测试第一个字母。要测试另一个字母,您必须对列表中的其他项目使用递归继续迭代,直到完成所有字母。

祝你好运!

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) 

我被你的使用利弊假设你可能还没有被允许使用本地表达式或列表缩写。该解决方案可以为您的家庭作业是比较合适的:

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

你限制使用某些特定的功能? 您可以使用正则表达式来处理这类事情。

(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