关于在 str 中查找元音的 Scheme racket 问题
Scheme racket question about finding vowels in str
我试图编写有关 get-vowels 及其 运行 100% 的代码,但我试图显示这 4 个输出,
我也只想显示这 4 个输出,但找不到解决方案。
> (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"))
4
示例:
输出:
4个
“ooaa”
您不应该将过程命名为 filter
,它与 完全按照您的意愿 做事的内置过程相冲突。事实上,你只需要删除你的 filter
实现,它就会起作用:)
(define vowels (string->list "aeiouyæøå"))
(define (vowel? c)
(member c vowels))
(define (count-vowels s)
(length (filter vowel? (string->list s))))
; there's a bit of code duplication here, you could
; refactor it if you want, to avoid repeating yourself
(define (extract-vowels s)
(list->string (filter vowel? (string->list s))))
(count-vowels "foobarbaz")
=> 4
(extract-vowels "foobarbaz")
=> "ooaa"
我试图编写有关 get-vowels 及其 运行 100% 的代码,但我试图显示这 4 个输出, 我也只想显示这 4 个输出,但找不到解决方案。
> (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"))
4
示例: 输出: 4个 “ooaa”
您不应该将过程命名为 filter
,它与 完全按照您的意愿 做事的内置过程相冲突。事实上,你只需要删除你的 filter
实现,它就会起作用:)
(define vowels (string->list "aeiouyæøå"))
(define (vowel? c)
(member c vowels))
(define (count-vowels s)
(length (filter vowel? (string->list s))))
; there's a bit of code duplication here, you could
; refactor it if you want, to avoid repeating yourself
(define (extract-vowels s)
(list->string (filter vowel? (string->list s))))
(count-vowels "foobarbaz")
=> 4
(extract-vowels "foobarbaz")
=> "ooaa"