读取字符串并使用方案计算数字元音

时间:2015-07-02 04:41:49

标签: scheme racket

编写一个名为count_vowel的过程,用于计算任何给定字符串中元音的数量(a,e,i,o,u)。

到目前为止,我有这个......

 (define (count_vowel (str)
                 (char-set-fold (count_vowel (ch i) (if (vowel? ch) (+ i 1) i)) 0 str))

但我收到此错误... "#%plain-lambda:不是(str)"

中的标识符

我需要使用DrRacket和R5RS作为语言。

3 个答案:

答案 0 :(得分:1)

试试这个:

(define (count-vowel str)
    (char-set-fold (lambda (ch i) 
                       (if (vowel? ch) 
                           (+ i 1)
                           i))
                   0 (string->char-set str)))

答案 1 :(得分:1)

您的define错了。

应该是:

(define (count_vowel str)
   .... )

答案 2 :(得分:1)

在原始解决方案中,请注意第二行的缩进是错误的。每当缩进错误时,都表明某处存在括号错误。在其他答案中指出了确切的错误。

由于char-set-fold是srfi函数,我认为是一个纯粹的R5RS解决方案 会有秩序的。我错误地认为filter是R5RS的一部分,因此 以下是一个简短的解决方案。事实证明,filter不是R5RS的一部分......

#lang r5rs

(define vowels (string->list "aeiouyæøå"))

(define (vowel? c)
  (member c vowels))

(define (filter p xs)
  (define (more) (filter p (cdr xs)))
  (cond
    ((null? xs)    '())
    ((p (car xs))  (cons (car xs) (more)))
    (else          (more))))


(define (count-vowels s)
  (length (filter vowel? (string->list s))))

(display (count-vowels "foobarbaz"))
(newline)
相关问题