Esittely latautuu. Ole hyvä ja odota

Esittely latautuu. Ole hyvä ja odota

Abstraktit tietotyypit – jatkoa TIEA341 Funktio-ohjelmointi 1 Syksy 2005.

Samankaltaiset esitykset


Esitys aiheesta: "Abstraktit tietotyypit – jatkoa TIEA341 Funktio-ohjelmointi 1 Syksy 2005."— Esityksen transkriptio:

1 Abstraktit tietotyypit – jatkoa TIEA341 Funktio-ohjelmointi 1 Syksy 2005

2 Viime luennolla ● ADT-käsite ● Pino ● Taulukko

3 Jono module Queue (Queue (), empty, isEmpty, enqueue, dequeue, peek) where empty :: Queue a isEmpty :: Queue a -> Bool enqueue :: a -> Queue a -> Queue a dequeue :: Queue a -> Queue a peek :: Queue a -> a

4 Jonon toteutus newtype Queue a = Q [a] empty = Q [] isEmpty (Q q) = null q enqueue e (Q q) = Q (e : q) dequeue (Q q) = Q (init q) peek (Q q) = last q Tehoton! dequeue ja peek ovat O(n)!

5 Jonon toteutus – jekku tekee hyvää data Queue a = Q [a] [a] empty = Q [] [] isEmpty (Q qb qf) = null qb && null qf enqueue e (Q qb qf) = Q (e : qb) qf dequeue (Q qb (_:qf)) = Q qb qf dequeue (Q qb@(_:_) []) = dequeue (Q [] (reverse qb)) peek (Q _ (e:_)) = e F. W. Burton: “An efficient implementation of FIFO queues”, Information Processing Letters, vol. 14, pp. 205–206, 1982. Kaksi listaa: - lista, jonka alku on jonon loppu - lista, jonka alu on jnon alku -tarvittaessa siirretään listasta toiseen

6 Joukot module Set (Set, empty, isEmpty. memb, unit, union, intersection, difference, subset) where empty :: Ord a => Set a isEmpty :: Ord a => Set a -> Bool memb :: Ord a => a -> Set a -> Bool unit :: Ord a => a -> Set a union, intersection, difference :: Ord a => Set a -> Set a -> Set a subset :: Ord a => Set a -> Set a -> Bool

7 Joukot – toteutus import BinTree newtype Set a = Set (BinTree a ()) empty = EmptyTree isEmpty (Set (BTEmpty)) = True isEmpty (Set (BTNode _ _ _ _)) = False memb e (Set s) = case BinTree.lookup e s of Just () -> True Nothing -> False...

8 Äärelliset kuvaukset module Map (Map, empty, isEmpty, hasKey insert, lookup) where empty :: Ord a => Map a b isEmpty :: Ord a => Map a b -> Bool hasKey :: Ord a => a -> Map a b -> Bool insert :: Ord a => a -> b -> Map a b -> Map a b lookup :: Ord a => a -> Map a b -> Maybe b

9 Vakiokirjastossa... ● module Array (eli module Data.Array) ● module Data.Set ● module Data.Map ● module Data.Queue

10 Haskellin vakiokirjaston taulukkotyyppi ● Sukua edellä esitellylle taulukkotyypille ● Mutta: pinnan alla tavallinen taulukko! – päivitys aiheuttaa koko taulukon kopioinnin! – siksi ei niin kovin käyttökelpoinen ● modulissa Array (sama kuin Data.Array) ● myös moniulotteiset taulukot mahdollisia ● tarkemmin: http://haskell.org/onlinereport/array.htmlhttp://haskell.org/onlinereport/array.html

11 module Data.Set ● Ks. http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Set.html http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Set.html

12 module Data.Map ● Ks. http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Map.html http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Map.html


Lataa ppt "Abstraktit tietotyypit – jatkoa TIEA341 Funktio-ohjelmointi 1 Syksy 2005."

Samankaltaiset esitykset


Iklan oleh Google