• P09

    From B. Pym@21:1/5 to Pascal Bourguignon on Sun Jun 9 13:32:32 2024
    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))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to B. Pym on Sun Jun 9 16:23:03 2024
    On 6/9/2024 6:32 AM, B. Pym wrote:
    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))





    (use srfi-1) ;; span

    (define (gp x)
    (if (null? x) '()
    (let-values (((F L) (span (cut equal? (car x) <>) x)))
    (cons F (gp L)))))

    (print (gp '(a b b a a a b b b b)))
    (print (gp '(c c c a d d d d a e e e e e)))


    (define (gpC x) (map (lambda (x) (list (car x) (length x))) (gp x)))

    (print (gpC '(a b b a a a b b b b)))
    (print (gpC '(c c c a d d d d a e e e e e)))


    There's no way to write (lambda (x) (list (car x) (length x)))
    using Cut or Cute???

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)