Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating interest in Haskell

Tags:

haskell

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.

like image 893
Moriz Büsing Avatar asked Dec 16 '22 08:12

Moriz Büsing


1 Answers

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.

like image 131
sepp2k Avatar answered Dec 22 '22 00:12

sepp2k