P10: 連続して出現する要素をまとめ、それぞれ出現回数を求める。
P09の成果を使う。
;;; ;; P10 (*) Run-length encoding of a list. ;; Use the result of problem P09 to implement the so-called run-length ;; encoding data compression method. Consecutive duplicates of elements ;; are encoded as lists (N E) where N is the number of duplicates of ;; the element E. ;; ;; Example: ;; * (encode '(a a a a b c c a a d e e e e)) ;; ((4 A) (1 B) (2 C) (2 A) (1 D)(4 E)) (defun encode (lis) (mapcar (lambda (packed) (list (length packed) (first packed))) (pack lis))) (encode '(a a a a b c c a a d e e e e)) ;; => ((4 A) (1 B) (2 C) (2 A) (1 D) (4 E)) (encode '(nil nil nil nil nil t t t t t)) ;; => ((5 NIL) (5 T)) (encode '(nil t t t t t nil)) ;; => ((1 NIL) (5 T) (1 NIL)) (encode nil) ;; => NIL