[Haskell-cafe] Area from [(x,y)] using foldl
Casey Hawthorne
caseyh at istar.ca
Sun Nov 8 17:05:58 EST 2009
How about these type signatures.
import Data.List
poly1 = [(0,1),(5,0),(3,4)]::[(Double,Double)]
areaPoly :: [(Double,Double)] -> Double
areaPolyCalc :: (Double,(Double,Double)) -> (Double,Double) ->
(Double,(Double,Double))
<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