3

我将如何制作一个分区函数,该函数将采用一个数字和一个列表将列表划分为较小的列表列表,其大小由数字给出,以便

Partition 3 '(a b c d e f g h) -> '((a b c) (d e f) (g h)) and etc. using take and drop?
4

1 回答 1

3

我会给你一些提示,以便你自己找到答案。填空:

(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和。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))
于 2013-03-04T00:49:12.643 回答