[Haskell-cafe] Area from [(x,y)] using foldl
Casey Hawthorne
caseyh at istar.ca
Sun Nov 8 17:13:38 EST 2009
How about these BETTER type signatures.
-- Area of a Polygon
import Data.List
type X = Double
type Y = Double
type Area = Double
poly1 = [(0,1),(5,0),(3,4)]::[(X,Y)]
areaPoly :: [(X,Y)] -> Area
areaPolyCalc :: (Area,(X,Y)) -> (X,Y) -> (Area,(X,Y))
<Spoiler Alert! -- Functions Below!>
areaPoly (pt:pts) = 0.5 * (fst (foldl' areaPolyCalc (0,pt) pts))
areaPolyCalc (sum,(x,y)) (xNext,yNext) = (sum + (x * yNext - xNext *
y),(xNext,yNext))
--
Regards,
Casey
More information about the Haskell-Cafe
mailing list