Removing vowels from a String (Scheme)

2019-08-06 04:16发布

I know the basic algorithm for this problem but I am having trouble changing the sentence into a list inside my conditional. I created make-list to make it easier on myself but I'm not sure where to put it in the code. For ex, in the first cond statement, I need the sentence to be a list before I check if the first element in the sentence is a vowel.. but I have been doing it syntactically wrong.

vowel-ci? returns #t if a character is a case insensitive vowel, and #f otherwise.

stenotype takes a sentence and returns it with all vowels removed.

(define make-list
   (lambda (string)
     (string->list string)))

(define stenotype
  (lambda (sentence)
    (cond
      [(vowel-ci? (car sentence)) (stenotype (cdr sentence))]
      [else (cons (car sentence) (stenotype (cdr sentence)))])))

标签: scheme
3条回答
做个烂人
2楼-- · 2019-08-06 04:56
(define make-list (string)

     (string->list string))

(cond
[(empty? make-list(sentence))empty]
      [(vowel-ci? (car make-list(sentence))) 
        (stenotype list->string ((cdr make-  list(sentence))))]
      [else (cons (car make-list(sentence)) (stenotype (cdr make-list(sentence))))])))
查看更多
干净又极端
3楼-- · 2019-08-06 05:04

You need to convert the string to a list only once and filter out the vowels using map or recursion. The following procedure shows how to use map:

(define (remove-vowels str) 
    (let ((res ()))
      (map (lambda (c) (if (not (vowel? c)) (set! res (append res (list c))))) 
           (string->list str))
      (list->string res)))

This one is recursive and avoids set! and append:

(define (remove-vowels str)
    (let loop ((slist (string->list str)) (res ()))
      (if (not (null? slist))
          (if (vowel-ci? (car slist))
              (loop (cdr slist) res)
              (loop (cdr slist) (cons (car slist) res)))
          (list->string (reverse res)))))

Usage:

> (remove-vowels "hello, world")
"hll, wrld"
> (remove-vowels "goodbye cruel world")
"gdby crl wrld"
查看更多
我只想做你的唯一
4楼-- · 2019-08-06 05:11

There are a few different tasks (preparing input so it can be processed by your implementation and the processing itself), which you've broken into two different functions. The next step is combining the functions, rather than rewriting the latter to use the former. The simplest way of combining functions is composition. Compose make-list and stenotype (you may wish to name this composition) and you'll have your solution.

(define double
    (lambda (x) (* x 2)))

(define inc
    (lambda (x) (+ x 1)))

; one option: define a new function that's a composition    
(define double-inc
    (lambda (x) (inc (double x))))

; another option: compose the functions when you use them
(inc (double 23))

; yet another option: make the other functions local to the composition
; Useful if the other functions are subordinate to the composition, and 
; aren't useful outside of it. You often see this with recursive functions,
; where the outer function sets up a call to the recursive function
(define (double-inc x)
    (define (double x) (* x 2))
    (define (inc x) (+ x 1))
  (inc (double x)))

(define (max numbers)
    (define (max-recur maximum numbers)
      (cond ((eq? numbers '()) maximum)
            ((< maximum (car numbers)) (max-recur (car numbers) (cdr numbers)))
            (else (max-recur maximum (cdr numbers)))))
  (max-recur (car numbers) (cdr numbers)))

Note that you're missing a base case in stenotype to end the recursion.

查看更多
登录 后发表回答