1

約 10 時間後に予定されている割り当て作業を行っているときに、少し問題があります。

関数 has-vowels を作成することになっていますか? 文字列を消費し、文字列に母音があるかどうかに応じて true または false を返します。

例 (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」が true で「www」が false ですが、「wow」などの混合文字列も false ですか?

ヒントやヒントはありますか?

ありがとう!

4

4 に答える 4

1

単語の残りの部分はチェックしていません。最初の文字だけをチェックしています。したがって、「oio」は o が母音であるため機能し、「www」は w が母音ではないため機能しません。また、「wow」も w が母音ではないため機能しません。

ヒントとして、リストが空ではなく、最初の文字が母音でない場合に何が起こるかを変更する必要があります。現在、false を返すだけです。

于 2013-02-27T04:57:32.993 に答える
1

あなたのコードは意味がありません。まず、基本ケースを説明する必要があります。空の場合は false を返す必要があります。

(空? 母音) true) は間違っています。ここで、空の場合は true を返す必要があると言っていますが、これは正しくありません。

また、Warwick Masson が前述したように、最初の文字のみをテストします。他の文字をテストするには、リスト内の他の項目で再帰を使用する必要があり、すべての文字を調べるまで繰り返します。

幸運を!

于 2013-02-27T08:00:39.387 に答える
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
于 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)

コンスの使用により、ローカル式またはリストの省略形を使用することがまだ許可されていない可能性があると想定しています。このソリューションは、宿題により適している場合があります。

;; 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 に答える