2017-04-16 42 views
-1

我想編寫一個給定列表和數字的程序,將列表分割成給定數目的列表。球拍,在給定數量的列表中分割一個列表

例如,

(split 3 '(1 2 3 4 5 6 7 8 9 10)) 

應該讓三個列表(例如,因爲名單是隨機進行):

‘(4 1 6) 

‘(9 7 2) 

‘(3 10 8 5) 

應該具備的要素的隨便挑,這樣的創建列表總是不同的。

一旦我有了代碼,我會將它與這個我得到的in another question at this web進行比較。 (我正在學習球拍)

+0

你到目前爲止嘗試了什麼? – Renzo

回答

0
(define (n-way-split n lst) 
    (define (go len* lst) 
    (match len* 
     ['() '()] 
     [`(,len . ,len*-tail) 
     (let-values ([(next-len-elems rest-of-elems) (split-at lst len)]) 
     (cons next-len-elems (go len*-tail rest-of-elems)))])) 
    (go (n-way-split-length* n (length lst)) 
     (shuffle lst))) 

(define (n-way-split-length* n len) 
    (let*-values ([(quot rem)      (quotient/remainder len n)] 
       [(count/lists-of-length-quot) (- n rem)] 
       [(count/lists-of-length-quot+1) rem] 
       [(size*) (append (make-list count/lists-of-length-quot quot) 
           (make-list count/lists-of-length-quot+1 (+ 1 quot)))]) 
    size*)) 
+0

非常感謝Brendam!這正是我想要的,它確實對我有幫助。真的謝謝你! –