I have just started playing around with
functional programming. I really like the language but I do have trouble implementing very simply calculcations (which is likely due to my little experience for functional languages). Hopefully some of you experts can help me improve my understanding of the language.
I have posted the below on stackoverflow but wasn’t sure if it makes sense to post the question here as well.
My below code takes as input
wacc : Float and
cfs : List Float and should calculate a net preset value (i.e. for each element of cfs calculate cfs_i / (1 + wacc)^i and then calculate the sum of the values).
The code works but is very verbose and potentially not idiomatic.
My main question besides hints how to make it more concise / idiomatic is how do I change my code to be able to accept wacc and cfs of
Helpful for any hint / info. Thanks!
-- helper functions zip : List a -> List b -> List (a,b) zip list1 list2 = List.map2 Tuple.pair list1 list2 calcDF : Float -> Int -> List Float calcDF wacc n = let waccs = List.repeat n wacc time = List.range 0 n |> List.map toFloat waccs_time = zip waccs time in List.map (\x -> 1/ (1 + Tuple.first x)^(Tuple.second x)) waccs_time -- my npv function calcNPV : List Float -> Float -> Html text calcNPV cfs wacc = let n = List.length cfs df = calcDF wacc n cfs_df = zip cfs df in List.map (\x -> (Tuple.first x) * (Tuple.second x)) cfs_df |> List.foldl (+) 0
calcNPV [100,100,100] 0.1 -- returns 273.553719
Thanks for your help!