<table cellspacing="0" cellpadding="0" border="0" ><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></table><br>