From 4588333f1329bc1f217688588aeea628357e4c34 Mon Sep 17 00:00:00 2001 From: Ari Angga Nugraha <ari.angga@ui.ac.id> Date: Sun, 27 Dec 2020 14:17:15 +0700 Subject: [PATCH] implement type system --- src/NetPresentValue.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/NetPresentValue.hs b/src/NetPresentValue.hs index af73ac8..735ed01 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] -- GitLab