我该如何创建一个分区函数,该函数需要一个数字和一个列表,以便将列表划分为更小的列表列表,列表的大小由数字给定,以便
Partition 3 '(a b c d e f g h) -> '((a b c) (d e f) (g h)) and etc. using take and drop?发布于 2013-03-04 08:49:13
我会给你一些提示,这样你就可以自己找到答案。填空:
(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))https://stackoverflow.com/questions/15192573
复制相似问题