<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Continuing with continuations</title>
	<atom:link href="http://therning.org/magnus/archives/306/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus/archives/306</link>
	<description>Incoherent mumblings</description>
	<pubDate>Fri, 05 Dec 2008 11:12:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: C.K.</title>
		<link>http://therning.org/magnus/archives/306#comment-74703</link>
		<dc:creator>C.K.</dc:creator>
		<pubDate>Mon, 23 Jul 2007 14:51:21 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/306#comment-74703</guid>
		<description>&lt;p&gt;And if all you want is to jump out of the current chain of (&#62;&#62;=), then you don't need all the complexity of continuations.&lt;/p&gt;

&lt;p&gt;For example: In playing with the DNA to RNA procedure from the ICFP2007 spec, there is a need to break out of the code to the end of the iteration.&lt;/p&gt;

&lt;p&gt;I used a "MaybeT" as the simplest solution [1].  Thus I could run (forever $ oneIter) and it would break out as needed by using the fail _ == mzero == Nothing command.&lt;/p&gt;

&lt;p&gt;If you need to pass out a result instead of a plain Nothing, then using ErrorT from Control.Monad.Error [2] which is actually 'Either' so (Left foo) is more informative than Nothing.&lt;/p&gt;

&lt;p&gt;[1] http://haskell.org/haskellwiki/New_monads/MaybeT
[2] http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>And if all you want is to jump out of the current chain of (&gt;&gt;=), then you don&#8217;t need all the complexity of continuations.</p>
<p>For example: In playing with the DNA to RNA procedure from the ICFP2007 spec, there is a need to break out of the code to the end of the iteration.</p>
<p>I used a &#8220;MaybeT&#8221; as the simplest solution [1].  Thus I could run (forever $ oneIter) and it would break out as needed by using the fail _ == mzero == Nothing command.</p>
<p>If you need to pass out a result instead of a plain Nothing, then using ErrorT from Control.Monad.Error [2] which is actually &#8216;Either&#8217; so (Left foo) is more informative than Nothing.</p>
<p>[1] <a href="http://haskell.org/haskellwiki/New_monads/MaybeT" rel="nofollow">http://haskell.org/haskellwiki/New_monads/MaybeT</a><br />
[2] <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html" rel="nofollow">http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/306#comment-74464</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Sat, 21 Jul 2007 22:21:45 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/306#comment-74464</guid>
		<description>&lt;p&gt;dolio, thanks for the comment.  I did miss Cale's comment about breaking out of &lt;code&gt;forever&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You've also prompted me to look at &lt;code&gt;Reader&lt;/code&gt;, it's one of the many monads I haven't looked closer at.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>dolio, thanks for the comment.  I did miss Cale&#8217;s comment about breaking out of <code>forever</code>.</p>
<p>You&#8217;ve also prompted me to look at <code>Reader</code>, it&#8217;s one of the many monads I haven&#8217;t looked closer at.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gerel</title>
		<link>http://therning.org/magnus/archives/306#comment-71590</link>
		<dc:creator>gerel</dc:creator>
		<pubDate>Fri, 06 Jul 2007 02:21:16 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/306#comment-71590</guid>
		<description>&lt;p&gt;CPS is a much simpler concept than Haskell Monads a la Wikibooks:&lt;/p&gt;

&lt;p&gt;http://en.wikibooks.org/wiki/Haskell/Understanding_monads&lt;/p&gt;

&lt;p&gt;The analogy between nuclear waste (t) robots (&#62;&#62;=) and containers (M t) sucks a lot. If you come with some Lisp background and read that wikibook, your head collapses, seriously.&lt;/p&gt;

&lt;p&gt;I know this is kinda off-topic here but needed to say it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>CPS is a much simpler concept than Haskell Monads a la Wikibooks:</p>
<p><a href="http://en.wikibooks.org/wiki/Haskell/Understanding_monads" rel="nofollow">http://en.wikibooks.org/wiki/Haskell/Understanding_monads</a></p>
<p>The analogy between nuclear waste (t) robots (&gt;&gt;=) and containers (M t) sucks a lot. If you come with some Lisp background and read that wikibook, your head collapses, seriously.</p>
<p>I know this is kinda off-topic here but needed to say it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dolio</title>
		<link>http://therning.org/magnus/archives/306#comment-71585</link>
		<dc:creator>dolio</dc:creator>
		<pubDate>Fri, 06 Jul 2007 01:29:46 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/306#comment-71585</guid>
		<description>&lt;p&gt;Not to disparage your efforts, but your code strikes me as rather weird. What you're essentially doing is, I think, at the end of each function, taking the continuation, and sticking something right before it (although that thing may in turn stick something right before it as well). That seems like no more than writing mutually recursive functions, only in a more verbose manner.&lt;/p&gt;

&lt;p&gt;The real neat thing about continuations, in my opinion, is that you can use 'forever', but still give the functions inside the loop the chance to break out (which I seem to recall Cale mentioning yesterday, but maybe it got lost in the shuffle). For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foreverK fm = callCC (\k -&#62; forever (fm k))

getCharCC break = do c &#60;- liftIO getChar
                     when (c == 'q') $ break ()
                     return c

putCharCC break c = do liftIO $ putChar c
                       when (c == 'x') $ break ()

getCharCC' break = do c &#60;- liftIO getChar
                      when (c == 'q') $ break ()
                      putCharCC' break c

putCharCC' break c = do liftIO $ putChar c
                        when (c == 'x') $ break ()
                        getCharCC' break

test1 = foreverK (\k -&#62; getCharCC k &#62;&#62;= putCharCC k)

test2 = callCC getCharCC'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, there are two versions; one uses forever and the other just has mutually recursive functions. They'd both loop forever, &lt;em&gt;except&lt;/em&gt; that we call 'callCC' outside of the infinite loop, and pass it in (as break), so that when one of the functions detects the exiting condition, it can return to the continuation of (essentially) the infinite loop itself, and terminate.&lt;/p&gt;

&lt;p&gt;You can also use the reader transformer to make things a bit less noisy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foreverK' m = callCC $ runReaderT (forever m)

getCharCC'' = do c &#60;- liftIO getChar
                 when (c == 'q') breakCC
                 return c

putCharCC'' c = do liftIO $ putChar c
                   when (c == 'x') breakCC

breakCC = ask &#62;&#62;= \k -&#62; lift $ k ()

test3 = foreverK' (getCharCC'' &#62;&#62;= putCharCC'')
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Not to disparage your efforts, but your code strikes me as rather weird. What you&#8217;re essentially doing is, I think, at the end of each function, taking the continuation, and sticking something right before it (although that thing may in turn stick something right before it as well). That seems like no more than writing mutually recursive functions, only in a more verbose manner.</p>
<p>The real neat thing about continuations, in my opinion, is that you can use &#8216;forever&#8217;, but still give the functions inside the loop the chance to break out (which I seem to recall Cale mentioning yesterday, but maybe it got lost in the shuffle). For example:</p>
<pre><code>foreverK fm = callCC (\k -&gt; forever (fm k))

getCharCC break = do c &lt;- liftIO getChar
                     when (c == 'q') $ break ()
                     return c

putCharCC break c = do liftIO $ putChar c
                       when (c == 'x') $ break ()

getCharCC' break = do c &lt;- liftIO getChar
                      when (c == 'q') $ break ()
                      putCharCC' break c

putCharCC' break c = do liftIO $ putChar c
                        when (c == 'x') $ break ()
                        getCharCC' break

test1 = foreverK (\k -&gt; getCharCC k &gt;&gt;= putCharCC k)

test2 = callCC getCharCC'
</code></pre>
<p>So, there are two versions; one uses forever and the other just has mutually recursive functions. They&#8217;d both loop forever, <em>except</em> that we call &#8216;callCC&#8217; outside of the infinite loop, and pass it in (as break), so that when one of the functions detects the exiting condition, it can return to the continuation of (essentially) the infinite loop itself, and terminate.</p>
<p>You can also use the reader transformer to make things a bit less noisy:</p>
<pre><code>foreverK' m = callCC $ runReaderT (forever m)

getCharCC'' = do c &lt;- liftIO getChar
                 when (c == 'q') breakCC
                 return c

putCharCC'' c = do liftIO $ putChar c
                   when (c == 'x') breakCC

breakCC = ask &gt;&gt;= \k -&gt; lift $ k ()

test3 = foreverK' (getCharCC'' &gt;&gt;= putCharCC'')
</code></pre>
]]></content:encoded>
	</item>
</channel>
</rss>
