Loading [MathJax]/extensions/tex2jax.js

2010年7月15日木曜日

Decode a run-length encoded list

;; P12 (**) Decode a run-length encoded list.
;; Given a run-length code list generated as specified in problem P11. Construct its uncompressed version.
(define (decode lst)
(let loop ((lst lst) (acc '()))
(if (null? lst)
(reverse acc)
(let ((head (car lst)))
(loop (cdr lst) (append
(if (pair? head)
(decode-aux head)
`(,head)) acc))))))
(define (decode-aux lst)
(let ((n (car lst)) (symb (cadr lst)))
(let loop ((n n) (acc '()))
(if (zero? n)
acc
(loop (- n 1) (cons symb acc))))))
view raw p12.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿