Skip to content

Instantly share code, notes, and snippets.

@jkachmar
Last active July 13, 2016 15:08
Show Gist options
  • Save jkachmar/e4fb7b3722c41050c526fc91e67ade5d to your computer and use it in GitHub Desktop.
Save jkachmar/e4fb7b3722c41050c526fc91e67ade5d to your computer and use it in GitHub Desktop.
Taylor Series Expansion of e^x
-- Power series expansion of the exponential function
-- i.e. e^x = 1 + x + (x^2)/2! + ... (x^n)/n!
-- 'Cute' version
exp :: Double -> Double
exp x = sum $ take 10 $ map go [0..]
where fac = product . flip take [1..]
go n = x ^ n / fac n
-- Explanatory version
exp' :: Double -> Double
exp' x = sum (take 10 (map go [0..]))
where fac :: Int -> Double
fac n = product (take n [1..])
go :: Int -> Double
go n = (x ^ n) / (fac n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment