<div class="gmail_quote">On Sat, May 2, 2009 at 3:13 AM, Anatoly Yakovenko <span dir="ltr"><<a href="mailto:aeyakovenko@gmail.com">aeyakovenko@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">> Though I don't fully understand what you are doing (specifically what you<br>
> mean by "specific order"), but in a lazy language, traversals are usually<br>
> simply encoded as lists. Just write a function which returns all the leaves<br>
> as a list, and filter over it.<br>
<br>
</div>yea, i know, i am trying to learn how to use the Cont monad. or<br>
continuation in haskell. The idea is that while i am processing some<br>
data i may hit a point whree some dependency isn't met and i want to<br>
take a different branch via continuation. I expect that branch to<br>
furfill my dependency and when its done i want to continue down the<br>
original branch</blockquote><div><br></div><div>Ah I see. Well, in my opinion, Cont is almost never the right answer. Others, who have an easier time thinking about continuations, may differ.</div><div><br></div><div>In any case, you are stating your problem very imperatively, which may be why you feel inclined to use continuations. E.g. "when it's done I want to continue down the original branch" is talking about control flow.</div>
<div><br></div><div>Maybe you really just want to do a topological sort of some data in your tree?</div><div><br></div><div>How is the tree structure related to the dependencies? How is the tree structure related to your traversal? E.g. are you using a combining function on each branch to a value over its subtrees?</div>
<div><br></div><div>Basically I think "do a traversal" is not enough information to answer your question. What is the relationship of the contents of the tree to the <i>contents </i>of the traversal?</div><div>
<br></div><div>Luke</div><div><br></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
<div class="im"><br>
<br>
>> module TestCont where<br>
>> import Control.Monad.Cont<br>
>> import Control.Monad.Identity<br>
>> import Control.Monad.State.Lazy<br>
>><br>
>> --our stupid tree<br>
>> data Tree a = Tree [Tree a]<br>
>> | Leaf a<br>
>><br>
>> --traverse all the branches<br>
>> search (Tree ts) next = do<br>
>> mapM_ (\ ti -> (callCC (search ti))) ts<br>
>> next $ ()<br>
>><br>
>> search tt@(Leaf a) next = do<br>
>> cur <- lift get<br>
>> case ((cur + 1) == a) of<br>
>> True -> do --the current leaf is what we want, update the state and return<br>
<br>
</div>this is where i succeed in my current branch, so i can just do my thing and exit<br>
<div class="im"><br>
>> lift $ put a<br>
>> return $ ()<br>
>> False -> do --the current leaf is not what we want, continue first, then try again<br>
<br>
</div>this is where i fail, so i want to take the "other" branch first<br>
expecting it to fulfill my dependency.<br>
<div class="im"><br>
>> next ()<br>
>> search tt (\ _ -> error "fail")<br>
>><br>
>> t1 = Leaf 1<br>
>> t2 = Leaf 2<br>
>> t3 = Tree [t1,t2]<br>
>> t4 = Leaf 3<br>
>> t5::Tree Int = Tree [t4,t3]<br>
>><br>
>> run = runIdentity (runStateT ((runContT $ callCC (search t5)) return) 0)<br>
<br>
<br>
</div>but i think next doesn't do exactly what i think it does<br>
</blockquote></div><br>