asb: head /dev/brain > /dev/www

My home, musings, and wanderings on the world wide web.

99 LISP problems: Problem #10

Solution to the 99 LISP Problems #10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(defun pack-first (alist)
  (if (null alist)
    nil
    (if (equalp
          (car alist)
          (cadr alist))
      (append (list (car alist)) (pack-first (cdr alist)))
      (list (car alist)))))

(defun trim-first (alist)
  (if (null alist)
    nil
    (if (not (equalp
               (car alist)
               (cadr alist)))
      (cdr alist)
      (trim-first (cdr alist)))))

(defun encode (alist)
  (if (null alist)
    nil
    (append
      (list (list (length (pack-first alist)) (car alist)))
      (encode (trim-first alist)))))

Lisp dialect: Steel Bank Common Lisp