Funcional Programming is very new to me and can't seem to understand how to use a function as an argument for another function. finalvalue is supposed to calculate the final value after a period, and finalvalue2 after 2 periods.
interest :: Float -> Float -> Float
interest capital rate = capital * rate * 0.01
finalvalue :: Float -> Float -> Float
finalvalue capital rate = capital + interest capital rate
finalvalue2 :: Float -> Float -> Float
finalvalue2 capital rate = finalvalue capital rate + interest finalvalue capital rate rate
I get this:
Couldn't match expected type `Float'
against inferred type `Float -> Float -> Float'
In the first argument of `interest', namely `finalvalue'
In the second argument of `(+)', namely
`interest finalvalue capital rate rate'
In the expression:
finalvalue capital rate + interest finalvalue capital rate rate
I'm sure I'm missing a basic point here but I just can't find out what it is.
interest finalvalue capital rate rate
Here you're calling interest
with four arguments, the first of which is a function. Since interest
's first argument needs to be a Float, not a function, you get the error message you do.
What you probably intended to write was interest (finalvalue capital rate) rate
, which calls interest
with two floats, the first of which is the result of calling finalvalue
with capital
and rate
as arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With