2011-03-01 59 views
1

我知道這個問題的基本算法,但是我在將條件改變爲條件內的列表時遇到了問題。我創建了make-list來讓自己更容易,但我不確定將它放在代碼中的位置。例如,在第一個cond語句中,在檢查句子中的第一個元素是否是元音之前,我需要句子成爲一個列表。但是我一直在語法上做錯了。從字符串中刪除元音(Scheme)

元音字母?如果一個字符是不區分大小寫的元音,則返回#t;否則返回#f。

stenotype需要一個句子,並返回它與所有元音刪除。

(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)))]))) 
+0

是速記應該返回一個列表或串? – outis 2011-03-01 21:50:38

+1

請注意,'make-list'實際上是'string-> list'的別名,但如果它有助於您掌握解決方案,它就是一個有價值的目的。 – outis 2011-03-01 21:51:44

+0

stenotype返回刪除了所有元音的字符串。對。所以我需要再次將列表轉換爲一個字符串。 – mdegges 2011-03-01 21:55:45

回答

2

有幾個不同的任務(準備,因此它可以通過你的執行和處理自身處理輸入),你已經分成兩個不同的功能。下一步是結合功能,而不是重寫後者來使用前者。組合功能最簡單的方法就是組合。撰寫make-liststenotype(您可能希望命名此作品),您將獲得您的解決方案。

(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))) 

請注意,您在stenotype中缺少一個基本案例來結束遞歸。

+0

感謝您的幫助!我發誓我在一秒鐘之前就開始工作了,然後我搞砸了。哈。但是,這是一個很好的提示。我將stenotype改爲stenotype-helper,然後能夠像這樣將它們組合起來:(list-> string(stenotype(string-> list sentence))))) – mdegges 2011-03-01 22:10:42

1
(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))))]))) 
+0

哦,我明白了,你必須把它變成一個list @ every線。這很煩人:] – mdegges 2011-03-01 22:25:10

2

您需要將字符串轉換成列表只有一次,並篩選出使用map或遞歸的元音。下面的過程說明了如何使用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))) 

這一個是遞歸,避免set!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))))) 

用法:

> (remove-vowels "hello, world") 
"hll, wrld" 
> (remove-vowels "goodbye cruel world") 
"gdby crl wrld" 
+0

「使用映射或遞歸過濾元音......」使用'filter'怎麼樣? :) – knivil 2011-03-02 20:16:39

+0

@knivil標準方案中沒有'filter'過程。 (http://www.r6rs.org/final/html/r6rs/r6rs.html)。它可能在某些實現中作爲擴展提供。 – 2011-03-03 03:54:44

+0

Petite Chez Scheme中有過濾器,我正在使用的版本。實際上,在我提交答案後我們才瞭解到這一點。但從我的理解,過濾器給你你正在尋找..所以在這種情況下,它會返回元音,而不是刪除它們..除非(不(過濾器))存在。這會做到這一點。 – mdegges 2011-03-05 21:12:10