• Re: Exercises please

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Sat Jun 21 20:26:29 2025
    From Newsgroup: comp.lang.lisp

    Pascal J. Bourguignon wrote:

    and my solutions (still incomplete): http://www.informatimago.com/develop/lisp/l99/index.html

    Where we find:

    (---------------------------------------------------------------
    P13 (**) Run-length encoding of a list (direct solution).

    Example:
    * (encode-direct '(a a a a b c c a a d e e e e))
    ((4 A) B (2 C) (2 A) D (4 E))
    "

    ;; Iterative solution, uses only O(r) space:

    (defun encode-modified (list)
    (let ((result '())
    (count 0)
    (last-item nil))
    (labels ((collect-result ()
    (push (if (= 1 count)
    last-item
    (list count last-item))
    result))
    (new-item (item)
    (setf count 1
    last-item item))
    (same-item ()
    (incf count))
    (return-result ()
    (when (plusp count)
    (collect-result))
    (nreverse result)))
    (dolist (item list (return-result))
    (cond
    ((zerop count) (new-item item))
    ((eql item last-item) (same-item))
    (t (collect-result)
    (new-item item))))))) ---------------------------------------------------------------)

    Gauche Scheme

    (define (encode List)
    (if (null? List)
    ()
    (let
    ((tmp (fold
    (lambda(x accum)
    (if (equal? x (caar accum))
    (cons (cons x (car accum)) (cdr accum))
    (cons (list x) accum)))
    `((,(car List)))
    (cdr List))))
    (reverse
    (map
    (lambda(xs) (let1 len (length xs)
    (if (= 1 len) (car xs) (list len (car xs)))))
    tmp)))))

    gosh> (encode '(a a a a b c c a a d e e e e))
    ((4 a) b (2 c) (2 a) d (4 e))
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.lisp on Sat Jun 21 21:12:02 2025
    From Newsgroup: comp.lang.lisp

    On 2025-06-21, B. Pym <Nobody447095@here-nor-there.org> wrote:
    Pascal J. Bourguignon wrote:

    and my solutions (still incomplete):
    http://www.informatimago.com/develop/lisp/l99/index.html

    Where we find:

    (---------------------------------------------------------------
    P13 (**) Run-length encoding of a list (direct solution).

    Example:
    * (encode-direct '(a a a a b c c a a d e e e e))
    ((4 A) B (2 C) (2 A) D (4 E))
    "

    (flow '(a a a a b c c a a d e e e e)
    (partition-by identity)
    (map [juxt car len]))
    ((a 4) (b 1) (c 2) (a 2) (d 1) (e 4))

    Oops.

    (flow '(a a a a b c c a a d e e e e)
    (partition-by identity)
    (map [juxt len car]))
    ((4 a) (1 b) (2 c) (2 a) (1 d) (4 e))

    Oops.

    (flow '(a a a a b c c a a d e e e e)
    (partition-by identity)
    (mapcar [iff cdr [juxt len car] car]))
    ((4 a) b (2 c) (2 a) d (4 e))

    I get working code before I'm done properly scanning the requirements and can fix it in a few keystrokes.

    Shorter:


    ;; Iterative solution, uses only O(r) space:

    TL; DR.
    --- Synchronet 3.21d-Linux NewsLink 1.2