Racket 我可以在BSL中将列表分成多个部分吗?

Racket 我可以在BSL中将列表分成多个部分吗?,racket,racket-student-languages,Racket,Racket Student Languages,我有一个值列表,希望从中获取前x个值并创建(list(listof first x values)(listof next x values)等等,直到这个列表变为空…) 例如,给定此列表:(列表“a”“b”“c”“d”“e”“f”“g”“h”“t”) 返回以下内容:(列表a“b”c)(列表d“e”f)(列表g“h”t) 提前感谢:)记住列表的数据类型是什么。您的类可能正在执行以下操作: ;; A IntegerList is one of: ;; - '() ;; - (cons In

我有一个值列表,希望从中获取前x个值并创建(list(listof first x values)(listof next x values)等等,直到这个列表变为空…)

例如,给定此列表:
(列表“a”“b”“c”“d”“e”“f”“g”“h”“t”)
返回以下内容:
(列表a“b”c)(列表d“e”f)(列表g“h”t)


提前感谢:)

记住列表的数据类型是什么。您的类可能正在执行以下操作:

;; A IntegerList is one of:
;;   - '()
;;   - (cons Integer IntegerList)
因此,您的模板应该反映这种结构。我将解决基本情况(我们希望将一个整数列表转换为一个整数列表)

首先,我将
1列表
数据类型定义为:

;; a 1List is:
;;  - (cons Integer '())
接下来,功能的目的声明和签名将是:

;; Takes a list of integers and returns a list of 1Lists of the same integers
;; IntegerList -> 1List
(define (make-1list lst)
  ...)
好的,很酷。现在我们需要测试用例:

(check-expect (make-1list (list 1 2 3)) (list (list 1) (list 2) (list 3)))
(check-expect (make-1list (list)) (list))
(check-expect (make-1list (list 42)) (list (list 42)))
最后,我可以制作我的模板:

(define (make-1list lst)
    (cond [(null? lst) ...]
          [else        ... (first lst) ... (rest lst) ...]))
(请注意,有时可以先制作一些模板,以帮助您指导所需的测试。)

最后,我们可以填写我们的代码:

(define (make-1list lst)
  (cond [(null? lst) '()]
        [else (cons (list (first lst)) (make-1list (rest lst)))]))
最后,这些示例也是测试,所以我们只需要运行它们来确保一切正常

现在,由于您想制作
3List
s而不是
1List
s,您知道如何按照此方法解决问题吗

  • 写下你的数据定义
  • 做出你的目标声明并签字
  • 举出你的例子
  • 制作你的模板
  • 编写实际函数
  • 将现有示例转换为测试

  • 遵循此模式可以帮助您将问题分解为更小的步骤。祝您好运。

    完成此任务的更好方法是使用累加器和递归