2013-02-27 50 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