History data structure suggestion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

History data structure suggestion

Victor Nazarov-2
Hello,

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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: History data structure suggestion

Günther Schmidt
Hi Victor,

actually I think this might be the application domain of the "Zipper"
data structure.

I suggest you just google for it, but there's also a post in this list,
a week or so back.

Günther

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe