<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">That last definition multiplied zeros coming out of the recursion, but I believe this one is good:<br><br>myprod l = prod l id id<br>&nbsp; where<br>&nbsp;&nbsp;&nbsp; prod [] k b = k 1<br>&nbsp;&nbsp;&nbsp; prod (x:xs) k b = if x == 0 then b 0 else prod xs (\ z -&gt; k (x * z)) b <br><br>My goal was to use CPS to do it. Did I succeed?<br><br>Also, is there another way to do the same thing without passing b through all those recursions? I'm not good enough with Haskell's syntax to see how to do it.<br><br>Michael<br><br>--- On <b>Mon, 4/20/09, michael rice <i>&lt;nowgate@yahoo.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: michael rice &lt;nowgate@yahoo.com&gt;<br>Subject: Re: [Haskell-cafe] CPS and the product function<br>To: haskell-cafe@haskell.org, "Tillmann Rendel"
 &lt;rendel@cs.au.dk&gt;<br>Date: Monday, April 20, 2009, 11:28 AM<br><br><div id="yiv964318149"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; font-size: inherit; line-height: inherit; font-size-adjust: inherit; font-stretch: inherit; -x-system-font: none;" valign="top">This also seems to work:<br><br>myprod l = prod l id<br>&nbsp; where<br>&nbsp;&nbsp;&nbsp; prod [] k = k 1<br>&nbsp;&nbsp;&nbsp; prod (x:xs) k = if x == 0 then 0 else prod xs (\ z -&gt; k (x * z))<br><br>*Main Data.List&gt; :load prod<br>[1 of 1] Compiling Main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( prod.hs, interpreted )<br>Ok, modules loaded: Main.<br>*Main Data.List&gt; myprod [1,2,3,4,5,0,6,7,8,9]<br>0<br>*Main Data.List&gt; myprod [1,2,3,4,5]<br>120<br>*Main Data.List&gt; myprod [0..]<br>0<br>*Main Data.List&gt;
 <br><br>Michael<br><br>--- On <b>Mon, 4/20/09, Tillmann Rendel <i>&lt;rendel@cs.au.dk&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Tillmann Rendel &lt;rendel@cs.au.dk&gt;<br>Subject: Re: [Haskell-cafe] CPS and the product
 function<br>To: haskell-cafe@haskell.org<br>Date: Monday, April 20, 2009, 11:07 AM<br><br><div class="plainMail">michael rice wrote:<br>&gt; I've been looking at CPS in Haskell and wondering how many multiplications the product function performs if it encounters a zero somewhere in the input list. Zero?<br>&gt; <br>&gt; Does anyone know the definition of the product function?<br><br>You can use Hoogle [1] to search for product [2]. The documentation page [3] has a link to the source code [4].<br><br><br>Depending on some flag, it is either<br><br>&nbsp; product = foldl (*) 1<br><br>or an explicit loop with an accumulator. That means that even for a non-strict (*), the whole input list would be processed before a result could be returned.<br><br>&gt; Does anyone know how to define it to avoid that?<br><br>You have to define a multiplication function which is non-strict in the second argument if the first is 0.<br><br>&nbsp; mult 0 b = 0<br>&nbsp; mult a
 b = a * b<br><br>Now we can foldr this multiplication function over a list, and evaluation will stop at the first 0.<br><br>&nbsp; foldr mult 1 ([1..100] ++ [0 ..])<br><br>However, this solution seems not to run in constant space. We can write it with a strict accumulator to avoid this problem:<br><br>&nbsp; product = product' 1 where<br>&nbsp; &nbsp; product' acc []&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;= acc<br>&nbsp; &nbsp; product' acc (0 : xs) = 0<br>&nbsp; &nbsp; product' acc (x : xs) = (product' $! acc * x) xs<br><br>Tillmann<br><br>[1] <a rel="nofollow" target="_blank" href="http://www.haskell.org/hoogle/">http://www.haskell.org/hoogle/</a><br>[2] <a rel="nofollow" target="_blank" href="http://www.haskell.org/hoogle/?hoogle=product">http://www.haskell.org/hoogle/?hoogle=product</a><br>[3] <a rel="nofollow" target="_blank"
 href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:product">http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:product</a><br>[4] <a rel="nofollow" target="_blank" href="http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html#product">http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html#product</a><br><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a rel="nofollow">Haskell-Cafe@haskell.org</a><br><a rel="nofollow" target="_blank" href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></blockquote></td></tr></tbody></table><br>

      </div><br>-----Inline Attachment Follows-----<br><br><div class="plainMail">_______________________________________________<br>Haskell-Cafe mailing list<br><a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></blockquote></td></tr></table><br>