Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Purely Functional Data Structures

Purely Functional Data Structures

A look at numerous data structures in purely functional setting. Based on the work of Chris Okasaki. Also a brief study of amortized analysis.

Related haskell code: https://github.com/tejasbubane/functional-data-structures

Tejas Bubane

February 24, 2019
Tweet

More Decks by Tejas Bubane

Other Decks in Programming

Transcript

  1. PURELY FUNCTIONAL DATA STRUCTURES BASED ON THE WORK OF CHRIS

    OKASAKI BY TEJAS BUBANE BANGALORE FUNCTIONAL PROGRAMMING MEETUP https://github.com/tejasbubane/functional-data-structures
  2. RED-BLACK TREES data Color = Red | Black data RBTree

    a = Empty | Node Color (RBTree a) a (RBTree a)
  3. RED-BLACK TREES ▸No red node has red child ▸Every path

    from root to empty node contains same number of black nodes
  4. AMORTIZED ANALYSIS data Queue a = Queue [a] [a] check

    :: [a] -> [a] -> Queue a check [] r = Queue (reverse r) [] check f r = Queue f r Take slowest cost and distribute over faster ones
  5. BANKERS QUEUES check :: Queue a -> Queue a check

    q@(Queue lenf fs lenr rs) | lenf <= lenr = Queue (lenf + lenr) (fs ++ reverse rs) 0 [] | otherwise = q No pattern-matching on lists (fs & rs) so no evaluation.