[Haskell-cafe] Re: Zippers

Heinrich Apfelmus apfelmus at quantentunnel.de
Thu Mar 5 05:21:11 EST 2009


Cristiano Paris wrote:
> Ryan Ingram wrote:
>> ...
>> Here is the problem with your "update":
>>
>> tree = Fork (Leaf 1) (Leaf 2)
>> ztree = initZ tree
>>
>> test = fromJust $ do
>>    z1 <- moveLeft ztree
>>    let z2 = update z1 3
>>    z3 <- moveUp z2
>>    z4 <- moveLeft z3
>>    this z4
>>
>> I'd expect "test" to equal 3, but I believe with your code that it
>> still equals 1.  As apfelmus said, update needs to completely
>> re-construct the zipper structure with the tied knot, which defeats
>> the purpose of using a zipper in the first place.
> 
> I got it. I dont't know what your expression "tied knot" is referring
> to but I got the point.

In  doInitZ , you're basically using the  s  itself to define the
moveLeft  and  moveRight  fields of  s . You could as well write it as

  initZ t = doInitZ Nothing t
      where
      doInitZ c (Leaf a)     = ZContext c Nothing Nothing $ Just a
      doInitZ c t@(Fork l r) = s
           where s = ZContext c (Just $ doInitZ (Just s) l)
                                (Just $ doInitZ (Just s) r)
                                Nothing

Such self-reference is usually called "tying the knot", see also

  http://www.haskell.org/haskellwiki/Tying_the_Knot


Regards,
apfelmus

--
http://apfelmus.nfshost.com



More information about the Haskell-Cafe mailing list