The type of foldl is:<div>(b -> a -> b) -> b -> [a] -> b</div><div><br></div><div>What do you expect 'a' and 'b' to be in your algorithm?<br><br><div class="gmail_quote">2009/11/8 michael rice <span dir="ltr"><<a href="mailto:nowgate@yahoo.com">nowgate@yahoo.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit">Here's an (Fortran) algorithm for calculating an area, given one dimensional<br>
arrays of Xs and Ys. I wrote a recursive Haskell function that works, and one using<br>FOLDL that doesn't. Why would Haskell be "expecting" (t, t) out of ((*) (xold-x) (yold+y))?<br><br>Michael<br><br>====================<br>
<br> AREA = 0.0<br> XOLD = XVERT(NVERT)<br> YOLD = YVERT(NVERT)<br> DO 10 N = 1, NVERT<br> X = XVERT(N)<br> Y = YVERT(N)<br> AREA = AREA + (XOLD - X)*(YOLD + Y)<br> XOLD = X<br>
YOLD = Y<br>10
CONTINUE<br><br> AREA = 0.5*AREA<br><br>====================<br><br>area :: [(Double,Double)] -> Double<br>area ps = abs $ (/2) $ area' (last ps) ps<br> where area' _ [] = 0<br> area' (x0,y0) ((x,y):ps) = (x0-x)*(y0+y) + area' (x,y) ps<br>
<br><br><br>*Main> let p = [(0.0,0.0),(1.0,0.0),(1.0,1.0),(0.0,1.0),(0.0,0.0)]<br>*Main> area (last p) p<br>1.0<br>*Main> <br><br>====================<br><br>area :: [(Double,Double)] -> Double<br>area p = foldl (\ (xold,yold) (x,y) -> ((*) (xold-x) (yold+y))) 0 ((last p):p)<br>
<br><br>Prelude> :l area<br>[1 of 1] Compiling Main ( area.hs, interpreted )<br><br>area.hs:29:40:<br> Occurs check: cannot construct the infinite type: t =
(t, t)<br> Expected type: (t, t)<br> Inferred type: t<br> In the expression: ((*) (xold - x) (yold + y))<br> In the first argument of `foldl', namely<br> `(\ (xold, yold) (x, y) -> ((*) (xold - x) (yold + y)))'<br>
Failed, modules loaded: none.<br>Prelude> <br><br></td></tr></tbody></table><br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto: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>
<br></blockquote></div><br><br clear="all"><br>-- <br>Eugene Kirpichov<br>Web IR developer, <a href="http://market.yandex.ru">market.yandex.ru</a><br>
</div>