I need to define this function called total.
total :: (Int -> Int) -> Int -> Int
so that the total f is the function which at value n gives the sum
f0 + f1 + …. + fn
Thanks for any help! Please.
According to the book as an example I found out about function composition that:
twice f = (f . f)
Here, f is a function, and the result is f composed with itself. For this to work, it needs to have the same input and output type. So we have
twice :: (a -> a) -> a -> a
This states that twice takes one argument, a function of type (a -> a), and returns a result of the same type. For instance, if successor is the function to add one to an integer,
successor :: Int -> Int
successor n = n + 1
then
(twice successor) 12 ->(successor . successor) 12
-> successor (successor 12) -> 14
If I understand your question correctly, the following should be fine
However I guess you’ll learn more if you define the function recursively. What should
totalatn=0return? Well: That’s, by your definitionf 0. I.e.n=1? That’stotal f 1 = f 1 + total f 0 == f 1 + f 0.n=2:total f 2 == f 2 + total f 1.nin general?See the pattern? You can write this into plain Haskell.