2012-12-14 31 views
1

完成計劃過程(重複多次登錄),該過程包含一個非負Integer列表並返回一個列表,其中包含按順序表示的每個值次數等於其值 的次數。見下面的例子。你可以在你的解決方案使用反向。您也可以在解決方案和本頁面的後面使用helper pro cedures以獲得更多空間。在列表中按值的數量重複值

注意:這不是一個作業。這是一個實踐問題,我只是無法得到答案

+1

[你有什麼試過?](http://whathaveyoutried.com) –

回答

0

我會給你一些提示,所以你可以自己解決這個練習題,填補空白。你可以做的第一件,也可能是最有用的事情是用兩個程序分解問題。第一個,賦予了許多n需要生產具有i重複n列表的護理:

(define (repeat n i) 
    (if (zero? i)    ; if no more repeats are needed 
     <???>     ; return the empty list 
     (cons <???>    ; else cons `n` and advance the recursion 
      (repeat <???>  ; `n` is left unchanged 
        <???>)))) ; but `i` is diminished by one 

第二個過程使用前一個作爲輔助,遍歷輸入lst和所有的結果相結合通過repeat產生的子列表:

(define (repeats-a-lot lst) 
    (if (null? lst)      ; if the list is empty 
     <???>       ; return the empty list 
     (append (repeat <???> <???>)  ; append n repetitions of current element 
       (repeats-a-lot <???>)))) ; and advance the recursion over the list 

有解決這個問題的幾種可能的方式,一些發燒友(使用尾遞歸,使用摺疊過程,等等),但這個IMHO是最簡單的方法,在這個意義上它只需要瞭解一些基本的列表操作過程。無論如何,它按預期工作:

(repeats-a-lot '(1 2 3)) 
=> '(1 2 2 3 3 3)