diff --git a/src/NetPresentValue.hs b/src/NetPresentValue.hs index af73ac8c4961f693f73a9c38ef6312c9909d4397..735ed016b7b22e648f06a63a6cfc98f4ab172008 100644 --- a/src/NetPresentValue.hs +++ b/src/NetPresentValue.hs @@ -29,25 +29,29 @@ calculateNetPresentValue :: InputNetPresentValue -> Double calculateNetPresentValue inputNetPresentValue = netPresentValue inputNetPresentValue -- high order function +netPresentValue :: InputNetPresentValue -> Double netPresentValue inputNetPresentValue = foldl (+) 0 pv where pv = presentValue inputNetPresentValue -- high order function +presentValue :: InputNetPresentValue -> [Double] presentValue inputNetPresentValue = zipWith (*) (netcashflow income outcome) (factorpresentvalue (project_interest inputNetPresentValue)) where income = [income_1 inputNetPresentValue, income_2 inputNetPresentValue, income_3 inputNetPresentValue, income_4 inputNetPresentValue, income_5 inputNetPresentValue] outcome = [outcome_1 inputNetPresentValue, outcome_2 inputNetPresentValue, outcome_3 inputNetPresentValue, outcome_4 inputNetPresentValue, outcome_5 inputNetPresentValue] -- currying +-- partial evaluation +netcashflow :: [Double] -> [Double] -> [Double] netcashflow = zipWith (-) --- recursion -power n 1 = n -power n m = n * (power n (m-1)) - --- recursion -powervalue interest n = 1 / (power (1 + interest) n) +-- pure function +powervalue :: Floating a => a -> a -> a +powervalue interest n = 1 / ((1 + interest)**n) -- high order function +-- anonymous function +-- polymorphic +factorpresentvalue :: (Floating b, Enum b) => b -> [b] factorpresentvalue interest = map (\x -> powervalue interest x) [1..5]