From f3aa8793b2f3fe2074d702e49f3d71771e6f4b38 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Thu, 16 Sep 2010 23:57:56 +0530 Subject: [PATCH] solution to 2.69 --- src/sicp/ex2_69.rkt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/sicp/ex2_69.rkt 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 -- 2.45.2