<< Previous exercise (2.69) | Index | Next exercise (2.71) >>

The book ignores newline characters printed in the text. I decided that they also should be encoded and came up with this:

```
(let*
([message
(string-replace
(string-append
"Get a job\n"
"Sha na na na na na na na na\n"
"Get a job\n"
"Sha na na na na na na na na\n"
"Wah yip yip yip yip yip yip yip yip yip\n"
"Sha boom") "\n" " \n ")]
[symbols-message (map string->symbol (string-split (string-upcase message) " "))]
;; this list of count pairs would have to be calculated by the programm actually ...
[huffman-tree (generate-huffman-tree (list
(cons 'BOOM 1)
(cons 'WAH 1)
(cons 'A 2)
(cons 'GET 2)
(cons 'JOB 2)
(cons 'SHA 3)
(cons (string->symbol "\n") 5)
(cons 'YIP 9)
(cons 'NA 16)))])
(display huffman-tree) (newline)
(display (encode symbols-message huffman-tree)) (newline)
(display (length (encode symbols-message huffman-tree))) (newline))
```

My code gives:

We can then encode the song:

And compare the length of the encoded message vs. its fixed-length version:

A 22% gain in size seems to be coherent.