首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Scheme上分区列表

在Scheme上分区列表
EN

Stack Overflow用户
提问于 2013-03-04 08:12:35
回答 1查看 920关注 0票数 3

我该如何创建一个分区函数,该函数需要一个数字和一个列表,以便将列表划分为更小的列表列表,列表的大小由数字给定,以便

代码语言:javascript
复制
Partition 3 '(a b c d e f g h) -> '((a b c) (d e f) (g h)) and etc. using take and drop?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-04 08:49:13

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

代码语言:javascript
复制
(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

显然,您必须按照问题描述中的要求在解决方案中的某处使用takedrop。像这样测试你的解决方案:

代码语言:javascript
复制
(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))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15192573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档