;; Nice recursive solution:
(defun group (list)
(labels ((group-run (element group list)
(cond
((null list) (list (cons element group)))
((eql element (first list)) (group-run element (cons element group) (rest list)))
(t (cons (cons element group) (group-run (first list) '() (rest list)))))))
(if (null list)
'()
(group-run (first list) '() (rest list)))))
;; Smartass solution, using Common Lisp reduce:
(defun group (list)
(reduce (lambda (item result)
(cond
((endp result) (list (list item)))
((eql (first (first result)) item) (cons (cons item (first result))
(rest result)))
(t (cons (list item) result))))
list
:from-end t
:initial-value '()))
P09 (**) Pack consecutive duplicates of list elements into sublists.
If a list contains repeated elements they should be placed in
separate sublists.
Example:
* (pack '(a a a a b c c a a d e e e e))
((A A A A) (B) (C C) (A A) (D) (E E E E))
Pascal Bourguignon wrote:
;; Nice recursive solution:
(defun group (list)
(labels ((group-run (element group list)
(cond
((null list) (list (cons element group)))
((eql element (first list)) (group-run element (cons element group) (rest list)))
(t (cons (cons element group) (group-run (first list) '() (rest list)))))))
(if (null list)
'()
(group-run (first list) '() (rest list)))))
;; Smartass solution, using Common Lisp reduce:
(defun group (list)
(reduce (lambda (item result)
(cond
((endp result) (list (list item)))
((eql (first (first result)) item) (cons (cons item (first result))
(rest result)))
(t (cons (list item) result))))
list
:from-end t
:initial-value '()))
Gauche Scheme
(use srfi-1) ;; span
(define (group lst)
(if (null? lst)
'()
(let-values (((a b) (span (cut equal? (car lst) <>) lst)))
(cons a (group b)))))
(group '(2 2 foo bar bar j j j k baz baz))
===>
((2 2) (foo) (bar bar) (j j j) (k) (baz baz))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 403 |
Nodes: | 16 (2 / 14) |
Uptime: | 42:32:46 |
Calls: | 8,407 |
Calls today: | 2 |
Files: | 13,171 |
Messages: | 5,904,945 |