数値とリストを使用して、リストを数値で指定されたサイズのリストの小さなリストに分割するパーティション関数を作成するにはどうすればよいですか?
Partition 3 '(a b c d e f g h) -> '((a b c) (d e f) (g h)) and etc. using take and drop?
自分で答えを見つけることができるように、いくつかのヒントを提供します。空欄を埋める:
(define (partition n lst)
(cond (<???> ; if the list is empty
<???>) ; then return the empty list
((< <???> n) ; if the lists' length is less than n
<???>) ; return a list with lst as its only element
(else ; otherwise
(cons ; cons a list with the result of
(<???> lst n) ; grabbing the first n elements of lst with
(<???> n ; the result of advancing the recursion and
(<???> lst n)))))) ; removing the first n elements of lst
明らかに、問題の説明で要求されているように、ソリューションのどこかでtake
andを使用する必要があります。drop
次のようにソリューションをテストします。
(partition 3 '(a b c d e f g h))
=> '((a b c) (d e f) (g h))
(partition 3 '(a b c d e f g h i))
=>'((a b c) (d e f) (g h i))