Skip to content

Instantly share code, notes, and snippets.

@acomminos
Created January 17, 2014 02:55
Show Gist options
  • Save acomminos/8467657 to your computer and use it in GitHub Desktop.
Save acomminos/8467657 to your computer and use it in GitHub Desktop.
Haskell Riemann Sums
data RiemannType = LeftSum | RightSum | MidSum
riemann :: RiemannType -> (Float -> Float) -> Float -> Float -> Float -> Float
riemann rt f a b n = dx * foldr (\l -> \r -> (f $ a + (l * dx)) + r) 0 range
where
range = case rt of
LeftSum -> [0..n-1]
RightSum -> [1..n]
MidSum -> [0.5..n-0.5]
dx = (b-a)/n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment