[Haskell-cafe] History data structure suggestion

Victor Nazarov asviraspossible at gmail.com
Sun Apr 12 17:52:07 EDT 2009


I have a gtk2hs application. I'd like to implement some kind of browser
history for it. It should have Back and Forward buttons and a popup menu
with radio items.History is a list of strings. There is a current position
in this list, so you can move this position back and forward.
Some kind of list zipper must be enough to implement this:
data History a = Empty | Hist { histBefore :: [a], histCurrent :: a,
histAfter :: [a] }

back, forward:: History a -> History a
back (Hist (b:bs) c as) = Hist bs b (c:as)
forward (Hist bs c (a:as)) = Hist (c:bs) a as

add :: a -> History a -> History a
add x (Hist bs c _) = Hist (c:bs) x [] --- When you add something, future
part of history becomes blank.
add x Empty = Hist [] x []

start :: History a -> History a
start (Hist bs c as) = Hist [] h (hs ++ [c] ++ as)
  where (h:hs) = reverse bs

But there is another requirement. There must be some "points" in the history
to store in popup menu. When you select any point the history sould be
rolled to this point.
For example.
data HistPoint a = HP Int

current :: History a -> HistPoint a
current (Hist bs _ _) = HP (length bs)

goTo :: HistPoint a -> History a -> History a
goTo (HP n) = foldr1 (.) (replicate n forward) . start

I wonder is there more effective, more easy to implement data structure to
represent History and HistPoint

Victor Nazarov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090413/0e759800/attachment.htm

More information about the Haskell-Cafe mailing list