From: Ramakrishnan Muthukrishnan Date: Thu, 16 Sep 2010 18:27:56 +0000 (+0530) Subject: solution to 2.69 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/biz//%22%3C?a=commitdiff_plain;h=f3aa8793b2f3fe2074d702e49f3d71771e6f4b38;p=sicp.git solution to 2.69 --- diff --git a/src/sicp/ex2_69.rkt b/src/sicp/ex2_69.rkt new file mode 100644 index 0000000..d169fb1 --- /dev/null +++ b/src/sicp/ex2_69.rkt @@ -0,0 +1,25 @@ +#lang racket + +(require rackunit + "ch2_3.rkt") + +(define (generate-huffman-tree pairs) + (successive-merge (make-leaf-set pairs))) + +(define (successive-merge set) + (cond [(empty? (rest set)) (first set)] + [else (successive-merge (adjoin-set (make-code-tree (first set) + (second set)) + (rest (rest set))))])) + +(test-begin + (let ([sample-tree (make-code-tree (make-leaf 'A 4) + (make-code-tree + (make-leaf 'B 2) + (make-code-tree (make-leaf 'D 1) + (make-leaf 'C 1))))]) + (check equal? + (generate-huffman-tree '((A 4) (B 2) (C 1) (D 1))) + sample-tree))) + +(provide generate-huffman-tree) \ No newline at end of file