11

在我正在使用 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 '()) -> '(())  

等等

我在 Racket 中提出了以下内容:

#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))  

这工作得很好,但我想知道如果没有更直接更简单的方法来做到这一点,那么我想知道 Racket 的表现力,某种方式来摆脱“带值调用”和需要反转列表在程序等方面,也许有些完全不同。

我的第一次尝试非常松散地基于“The Little Schemer”中的收集器模式,这比上面的更简单:

(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)]))) 

我正在寻找的是简单,简洁和直接的东西。

4

5 回答 5

4

这是我的做法:

;; 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)]))
于 2012-04-29T04:15:49.743 回答
3

我可以想到一个简单、直接的解决方案,它使用一个过程,只有原始列表操作和尾递归(no values, let-values, call-with-values) - 它非常有效。它适用于所有测试用例,代价是if在初始化期间添加几个表达式来处理空列表用例。由您决定这是否简洁:

(define (chunkify lst)
  (let ((lst (reverse lst))) ; it's easier if we reverse the input list first
    (let loop ((lst (if (null? lst) '() (cdr lst)))        ; list to chunkify
               (cur (if (null? lst) '() (list (car lst)))) ; current sub-list
               (acc '()))                                  ; accumulated answer
      (cond ((null? lst)                    ; is the input list empty?
             (cons cur acc))
            ((= (add1 (car lst)) (car cur)) ; is this a consecutive number?
             (loop (cdr lst) (cons (car lst) cur) acc))
            (else                           ; time to create a new sub-list
             (loop (cdr lst) (list (car lst)) (cons cur acc)))))))
于 2012-04-29T02:18:48.587 回答
3

还有另一种方法。

#lang racket

(define (split-between pred xs)
  (let loop ([xs xs]
             [ys '()]
             [xss '()])
    (match xs
      [(list)                 (reverse (cons (reverse ys) xss))]
      [(list x)               (reverse (cons (reverse (cons x ys)) xss))]
      [(list x1 x2 more ...)  (if (pred x1 x2) 
                                  (loop more (list x2) (cons (reverse (cons x1 ys)) xss))
                                  (loop (cons x2 more) (cons x1 ys) xss))])))

(define (consecutive? x y)
  (= (+ x 1) y))

(define (group-consecutives xs)
  (split-between (λ (x y) (not (consecutive? x y))) 
                 xs))


(group-consecutives '(1 2 3 5 6 7 9 10 11))
(group-consecutives '(1 2 3))
(group-consecutives '(1 3 4 5 7 9 10 11 13))
(group-consecutives '(1))
(group-consecutives '())
于 2012-04-29T15:26:35.357 回答
1

这是我的版本:

(define (chunkify lst)
  (let loop ([lst lst] [last #f] [resint '()] [resall '()])
    (if (empty? lst) 
        (append resall (list (reverse resint)))
        (begin
          (let ([ca (car lst)] [cd (cdr lst)])
            (if (or (not last) (= last (sub1 ca)))
                (loop cd ca (cons ca resint) resall)
                (loop cd ca (list ca) (append resall (list (reverse resint))))))))))

它也适用于最后一个测试用例。

于 2012-05-06T11:26:03.667 回答
1

我要玩。

从本质上讲,这与所提供的内容并没有太大的不同,但它确实将它放在 for/fold 循环方面。我越来越喜欢 for 循环,因为我认为它们可以生成更多“可见”(不一定可读)的代码。但是,(IMO - 哎呀)在熟悉球拍/方案的早期阶段,我认为最好坚持递归表达式。

(define (chunkify lst)  
    (define-syntax-rule (consecutive? n chunk)     
      (= (add1 (car chunk)) n))
    (if (null? lst)
        'special-case:no-chunks
        (reverse 
         (map reverse
              (for/fold  ([store    `((,(car lst)))])
                         ([n         (cdr lst)])
                 (let*([chunk   (car store)])
                   (cond 
                    [(consecutive? n chunk)
                        (cons  (cons n chunk)  (cdr store))]
                    [else
                        (cons  (list n)  (cons chunk (cdr store)))])))))))


(for-each
 (ƛ (lst)  
    (printf "input   :  ~s~n" lst)
    (printf "output  :  ~s~n~n" (chunkify lst)))
 '((1 2 3 5 6 7 9 10 11)
   (1 2 3)
   (1 3 4 5 7 9 10 11 13)
   (1)
   ()))
于 2012-05-05T23:21:08.747 回答