Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,38 @@
(define phrase "this is an example for huffman encoding")
; prepare initial probabilities table
(define table (ff->list
(fold (lambda (ff x)
(put ff x (+ (ff x 0) 1)))
{}
(string->runes phrase))))
; just sorter...
(define (resort l)
(sort (lambda (x y) (< (cdr x) (cdr y))) l))
; ...to sort table
(define table (resort table))
; build huffman tree
(define tree
(let loop ((table table))
(if (null? (cdr table))
(car table)
(loop (resort (cons
(cons
{ 1 (car table) 0 (cadr table)}
(+ (cdar table) (cdadr table)))
(cddr table)))))))
; huffman codes
(define codes
(map (lambda (i)
(call/cc (lambda (return)
(let loop ((prefix #null) (tree tree))
(if (ff? (car tree))
(begin
(loop (cons 0 prefix) ((car tree) 0))
(loop (cons 1 prefix) ((car tree) 1)))
(if (eq? (car tree) i)
(return (reverse prefix))))))))
(map car table)))

View file

@ -0,0 +1,10 @@
(print "weights: ---------------------------")
(for-each (lambda (ch)
(print (string (car ch)) ": " (cdr ch)))
(reverse table))
(print "codes: -----------------------------")
(map (lambda (char code)
(print (string char) ": " code))
(reverse (map car table))
(reverse codes))