]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_71.rkt
solutions to 4.35, 4.36 and 4.37
[sicp.git] / src / sicp / ex2_71.rkt
1 #lang racket
2
3 (require "ex2_69.rkt")
4
5 (define (range min max (step 1))
6   (if (>= min max)
7       '()
8       (cons min (range (+ min step) max step))))
9
10 ;; n = 5
11 (define tree5 (for/list ((x (range 0 5))
12                          (a '(A B C D E)))
13                 (list a (expt 2 x))))
14
15 (generate-huffman-tree tree5)
16 #|
17 Most frequently used symbol is E and has the code 1
18 Least frequently used symbol is A and has the code 0000
19 |#
20
21 ;; n = 10
22 (define tree10 (for/list ((x (range 0 10))
23                           (a '(A B C D E F G H I J)))
24                  (list a (expt 2 x))))
25
26 (generate-huffman-tree tree10)
27 #|
28 A - 000000000
29 J - 1
30 |#