An Alternative Data.List.Zipper
Hi, (I also sent this to libraries@, but without response; I'm posting here for a wider audience.) I'm somewhat of a beginner in Haskell, so take what I say with a grain of salt, please. The ListZipper implementation seems very odd to me, and #haskell seemed to agree with me. The current package implements a Zipper with
data Zipper = Zipper ![a] ![a]
which allows for empty zippers, among other things. Very strange to me. It also seems unnecessarily strict. There are also no expected typeclasses implemented, like Foldable or Traversable. I thought it would be interesting to try fixing these problems with a much cleaner design, and I'd like to share what I came up with: http://hpaste.org/14030 I'd very much appreciate some criticism of the code so that I can improve it. I'm not sure how best to provide this alternative on Hackage; should we make this merely a newer version of the old library, provide it in a conflicting package, or find a new namespace for it and provide it there? Thanks, Jeff Wheeler
This traverses the list three times (reverse, init and last are each linear time): fromListEnd xs = Zipper (reverse $ init xs) (last xs) [] But we only need to do it once: fromListEnd xs = let x:xs' = reverse xs in Zipper xs' x [] I don't *think* this has an effect on strictness/laziness, since both versions are strict in the spine of the list. --Max On Sat, Jan 17, 2009 at 10:33 AM, Jeff Wheeler <jeff@nokrev.com> wrote:
Hi,
(I also sent this to libraries@, but without response; I'm posting here for a wider audience.)
I'm somewhat of a beginner in Haskell, so take what I say with a grain of salt, please.
The ListZipper implementation seems very odd to me, and #haskell seemed to agree with me. The current package implements a Zipper with
data Zipper = Zipper ![a] ![a]
which allows for empty zippers, among other things. Very strange to me. It also seems unnecessarily strict. There are also no expected typeclasses implemented, like Foldable or Traversable.
I thought it would be interesting to try fixing these problems with a much cleaner design, and I'd like to share what I came up with:
I'd very much appreciate some criticism of the code so that I can improve it.
I'm not sure how best to provide this alternative on Hackage; should we make this merely a newer version of the old library, provide it in a conflicting package, or find a new namespace for it and provide it there?
Thanks,
Jeff Wheeler
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
On Sat, 2009-01-17 at 10:44 -0800, Max Rabkin wrote:
This traverses the list three times (reverse, init and last are each linear time): fromListEnd xs = Zipper (reverse $ init xs) (last xs) []
But we only need to do it once: fromListEnd xs = let x:xs' = reverse xs in Zipper xs' x []
I don't *think* this has an effect on strictness/laziness, since both versions are strict in the spine of the list.
Excellent suggestion; your solution is much more readable and faster. I've made the change here: http://hpaste.org/14030#a1 Thanks, Jeff Wheeler
participants (2)
-
Jeff Wheeler -
Max Rabkin