On Mon, Dec 8, 2008 at 1:40 PM, $BE<1;(B <span dir="ltr">&lt;<a href="mailto:littlesweetmelon@gmail.com">littlesweetmelon@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
I am confused by section 4.6 in Yet Another Haskell Tutorial written<br>
by Hal Daume III.<br>
The auther provides a CPS form of &quot;fold&quot; funtion, but I don&#39;t know:<br>
1. How to transform a normal function into CPS function. I need some<br>
hint in writting &quot;cfold&quot;.</blockquote><div><br>Normal function(s) return value(s).<br><br>CPS functions take the same value and uses/throws it as a parameter/argument to the &quot;extra&quot; argument, the Continuation.<br>
<br>so to transform a function into CPS just<br>1) make the function take one extra argument, the continuation (the &quot;rest&quot; of the process, next function in chain)<br>2) instead of returning a value, apply the continuation to the value (call the continuation with the result you would normally return)<br>
<br>Example (taken from <a href="http://en.wikibooks.org/wiki/Haskell/CPS">http://en.wikibooks.org/wiki/Haskell/CPS</a> , check it out)<br><br>square :: Int -&gt; Int<br>square x = x ^ 2<br><br>main = do<br>&nbsp; let x = square 4<br>
&nbsp; print x<br><br>in CPS :<br><br>squareCPS :: Int -&gt; (Int -&gt; a) -&gt; a<br>squareCPS x k = k (x ^ 2)<br><br>main = squareCPS 4 print<br><br>which shows that here, the print function is the continuation for the square function.<br>
instead of returning the value (16, or 4*4 here), the CPS variant feeds the value to the continuation (print here)<br><br>hope it helps.<br></div></div><br>