This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -28,17 +28,16 @@ hLength Nil = 0
hLength h = cnt h
hFromList :: (Ord a) => [a] -> MinHeap a
hFromList xs = hlist Nil xs where
hlist h [] = h
hlist h (x:xs) = hlist (hPush x h) xs
hFromList = foldl (flip hPush) Nil
hToList :: (Ord a) => MinHeap a -> [a]
hToList Nil = []
hToList h = x:hToList hh where (x,hh) = hPop h
hToList = unfoldr f where
f Nil = Nothing
f h = Just $ hPop h
main = mapM print $ (hToList (hFromList [
main = mapM_ print $ hToList $ hFromList [
(3, "Clear drains"),
(4, "Feed cat"),
(5, "Make tea"),
(1, "Solve RC tasks"),
(2, "Tax return")]))
(2, "Tax return")]

View file

@ -0,0 +1,24 @@
#lang racket
(require data/heap)
(define pq (make-heap (λ(x y) (<= (second x) (second y)))))
(define (insert! x pri)
(heap-add! pq (list pri x)))
(define (remove-min!)
(begin0
(first (heap-min pq))
(heap-remove-min! pq)))
(insert! 3 "Clear drains")
(insert! 4 "Feed cat")
(insert! 5 "Make tea")
(insert! 1 "Solve RC tasks")
(insert! 2 "Tax return")
(remove-min!)
(remove-min!)
(remove-min!)
(remove-min!)
(remove-min!)

View file

@ -0,0 +1,5 @@
"Solve RC tasks"
"Tax return"
"Clear drains"
"Feed cat"
"Make tea"