Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Huffman Tree in LISP

Name: Anonymous 2010-12-30 1:10


huff xs -> xs.sort.{[x@xs ys:_:@{l=>l.lhd!=x}]=>[[x@xs] @ys.rec]}
           |> map x~>[x.len x.lhd] |> sort by={a b => a|0 < b|0}
           |> {[x]=>x|1;[a b @xs]=>nins a|0+b|0 [a|1 b|1] xs |> rec}
           |> {bs x=>[[x bs]]; bs [a b]=>conc rec,[@bs 0],a rec,[@bs 1],b} []

usage example:

(repl "huff \"The algorithm for generating a Huffman tree is very simple.\"")
((\y (0 0 0 0 0))   (\h (0 0 0 0 1))   (\m (0 0 0 1))   
 (\n (0 0 1 0))     (\t (0 0 1 1))     (\f (0 1 0 0))   
 (\g (0 1 0 1))     (\e (0 1 1))       (\u (1 0 0 0 0 0))
 (\v (1 0 0 0 0 1)) (\. (1 0 0 0 1 0)) (\H (1 0 0 0 1 1))
 (\T (1 0 0 1 0 0)) (\p (1 0 0 1 0 1)) (\l (1 0 0 1 1)) 
 (\o (1 0 1 0 0))   (\s (1 0 1 0 1))   (\a (1 0 1 1))   
 (\i (1 1 0 0))     (\r (1 1 0 1))     (\Space (1 1 1))  )

Name: Anonymous 2010-12-30 8:54

Anyway, this `by` keyword is pretty neat...

(repl "class Person name age")
no
NIL
(repl "persons=:[10++(Person name=['Jacob 'Ethan 'Michael 'Isabella 'Emma 'Olivia].rand age=90.rand)]")
([:Person name=Jacob age=73]  [:Person name=Olivia age=21]
 [:Person name=Olivia age=59] [:Person name=Jacob age=16]
 [:Person name=Jacob age=49]  [:Person name=Olivia age=78]
 [:Person name=Emma age=13]   [:Person name=Ethan age=29]
 [:Person name=Michael age=2] [:Person name=Ethan age=32] )
NIL
(repl "sort by=age persons")
([:Person name=Michael age=2] [:Person name=Emma age=13] 
 [:Person name=Jacob age=16]  [:Person name=Olivia age=21]
 [:Person name=Ethan age=29]  [:Person name=Ethan age=32]
 [:Person name=Jacob age=49]  [:Person name=Olivia age=59]
 [:Person name=Jacob age=73]  [:Person name=Olivia age=78])
NIL
(repl "sort by=name persons")
([:Person name=Emma age=13]   [:Person name=Ethan age=29]
 [:Person name=Ethan age=32]  [:Person name=Jacob age=73]
 [:Person name=Jacob age=16]  [:Person name=Jacob age=49]
 [:Person name=Michael age=2] [:Person name=Olivia age=21]
 [:Person name=Olivia age=59] [:Person name=Olivia age=78])

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List