- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我正在 Racket 中处理的应用程序中,我需要获取一个数字列表并将该列表划分为连续数字的子列表:
(在实际应用中,我实际上是对由一个数字和一些数据组成的对进行分区,但原理是一样的。)
即如果我的程序被称为 chunkify
然后:
(chunkify '(1 2 3 5 6 7 9 10 11)) -> '((1 2 3) (5 6 7) (9 10 11))
(chunkify '(1 2 3)) -> '((1 2 3))
(chunkify '(1 3 4 5 7 9 10 11 13)) -> '((1) (3 4 5) (7) (9 10 11) (13))
(chunkify '(1)) -> '((1))
(chunkify '()) -> '(())
#lang racket
(define (chunkify lst)
(call-with-values
(lambda ()
(for/fold ([chunk '()] [tail '()]) ([cell (reverse lst)])
(cond
[(empty? chunk) (values (cons cell chunk) tail)]
[(equal? (add1 cell) (first chunk)) (values (cons cell chunk) tail)]
[else (values (list cell) (cons chunk tail))])))
cons))
(define (chunkify-list lst)
(define (lambda-to-chunkify-list chunk) (list chunk))
(let chunkify1 ([list-of-chunks '()]
[lst lst]
[collector lambda-to-chunkify-list])
(cond
[(empty? (rest lst)) (append list-of-chunks (collector (list (first lst))))]
[(equal? (add1 (first lst)) (second lst))
(chunkify1 list-of-chunks (rest lst)
(lambda (chunk) (collector (cons (first lst) chunk))))]
[else
(chunkify1 (append list-of-chunks
(collector (list (first lst)))) (rest lst) list)])))
最佳答案
这是我的做法:
;; chunkify : (listof number) -> (listof (non-empty-listof number))
;; Split list into maximal contiguous segments.
(define (chunkify lst)
(cond [(null? lst) null]
[else (chunkify/chunk (cdr lst) (list (car lst)))]))
;; chunkify/chunk : (listof number) (non-empty-listof number)
;; -> (listof (non-empty-listof number)
;; Continues chunkifying a list, given a partial chunk.
;; rchunk is the prefix of the current chunk seen so far, reversed
(define (chunkify/chunk lst rchunk)
(cond [(and (pair? lst)
(= (car lst) (add1 (car rchunk))))
(chunkify/chunk (cdr lst)
(cons (car lst) rchunk))]
[else (cons (reverse rchunk) (chunkify lst))]))
(chunkify '()) -> '() ;; not '(()), as you have
'(())
,然后我会重命名
chunkify
并编写一个专门处理空箱的包装器。
chunkify
在它上面,像这样:
;; chunkify : (listof number) -> (listof (non-empty-listof number))
;; Split list into maximal contiguous segments.
(define (chunkify lst)
(cond [(null? lst) null]
[else
(let-values ([(chunk tail) (get-chunk (cdr lst) (list (car lst)))])
(cons chunk (chunkify tail)))]))
;; get-chunk : (listof number) (non-empty-listof number)
;; -> (values (non-empty-listof number) (listof number))
;; Consumes a single chunk, returns chunk and unused tail.
;; rchunk is the prefix of the current chunk seen so far, reversed
(define (get-chunk lst rchunk)
(cond [(and (pair? lst)
(= (car lst) (add1 (car rchunk))))
(get-chunk (cdr lst)
(cons (car lst) rchunk))]
[else (values (reverse rchunk) lst)]))
关于scheme - 在 Racket 中对列表进行分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368835/
我意识到“强类型”和“软类型”的定义是松散且易于解释的,但我还没有找到关于无类型 Racket (根据我的理解意味着动态类型)和类型 Racket 的明确定义。 再一次,我确定它不是那么简单和干燥,但
我来自 Java 和 Python,很难理解面向对象代码在 Racket 中的工作方式。 给定 (define food% (class object% (super-new) (i
我安装了 Sublime Text - Racket Package,但没有用。出现此错误。我该怎么办。 [Errno 2] No such file or directory: 'racke
.rkt是“常规”Racket 源代码的常规文件扩展名。在文档中,我还看到了 .rktl 和 .rkts 正在使用。什么是.rktl和 .rkts用于,还有我不知道的更多 Racket 文件扩展名吗?
假设我为具有“中心”的集合定义了一个结构体。 (struct centered-set (center elems)) 我想保证以下条件。 elems是一个集合。 center是 elems 的成员(
我想要一个函数,它接受一个字符串,由一个 python 格式的树组成,就像这样 "[0, [1, 0]]" 并输出一个可用的 Racket 列表,像这样 '(0 (1 0)) 我的第一步是将字符串转换
是否有 Racket 内置函数列表?我正在寻找类似 this one 的列表对于 python 。我似乎无法在文档中找到一个。 最佳答案 这里有一些列表: Racket文档中所有函数的索引: http
虽然 Racket 允许创建 GUI 应用程序(甚至游戏),但我想知道它是否适合与 Things 一样在视觉上发布商业应用程序或 OmniOutliner .这样的应用程序会感觉原生还是看起来像一个不
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我是 Racket 的新手,尽管到目前为止我已经设法使用 servlet 并让一些东西发挥作用。我现在想做的是验证一个简单的用户名/密码 POST 请求。我已经通过“web-server/formle
我正在为现场编码表演创建一个程序,为此我需要一个基本的 S 表达式代码编辑器(我输入的内容将在适当的语法上下文中评估为 Racket 代码)。 由于 DrRacket 本身是用 Racket 编写的,
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我正在尝试在 Racket 中编写模块元语言 mylang ,它接受将修改后的主体传递给的第二种语言,例如: (module foo mylang typed/racket body) 相当于: (m
我没有运气找到一种方法来按照我想要的方式进行这项工作,所以如果有人能提供帮助,我将不胜感激。 我想要的是能够在终端上做到这一点: > racket 然后能够输入 Racket 命令并让它们编译。像,
我来自 Java 世界,对 Racket 充满热情。 想知道类似Maven的工具在哪里。如果你不熟悉Maven,还有Scala中的SBT,Android中的gradle,Elixir中的mix等,作为
在 typed/racket 我有一个像 [(? procedure? p ) (apply p xv*)]会导致错误: Type Checker: Function has no cases in:
我是 Lisp-scheme 的新手,对整个函数范式还很陌生,我目前正在做一个任务,要求我重载一个名称相同但参数集不同的函数 racket .下面是我试图实现的一个例子: #lang racket (
有没有办法在 Racket 中获取对象的唯一标识符?例如,当我们使用 Racket 的 eq?运算符来检查两个变量是否指向同一个对象,它使用什么标识符来实现这种比较? 我正在寻找类似 python 的
当我在 Dr. Racket 或从命令行运行我的测试时,失败的测试总是在一行上。 例如, (bad (interp-t-prog (list '(class posn extends object (
我有以下代码: (plot (function sin (- pi) pi #:label "y = sin(x)")) 但是在解决 SICP 时我没有遇到这样的结构:#:label "y = sin
我是一名优秀的程序员,十分优秀!