I was trying to figure out how to do something along the lines of
compositeFunctions :: [(a -> a)] -> (a -> a)
I was thinking I could use foldr
to continuously fold the list of functions over, but I can't figure anything out.
The composite function rule shows us a quicker way. If f(x) = h(g(x)) then f (x) = h (g(x)) × g (x). In words: differentiate the 'outside' function, and then multiply by the derivative of the 'inside' function. To apply this to f(x)=(x2 + 1)17, the outside function is h(·)=(·)17 and its derivative is 17(·)16.
You use composite functions whenever you buy a sale (discounted) item. When you are standing in the store trying to decide if you can afford the item, the first thing you calculate is the discount. For example, I want to buy this 20 dollar shirt, and it is on sale at 15% off.
In mathematics, function composition is an operation ∘ that takes two functions f and g, and produces a function h = g ∘ f such that h(x) = g(f(x)). In this operation, the function g is applied to the result of applying the function f to x.
foldr
does exactly what you want, since id
is the identity for (.)
(i.e., f . id == f
):
compose :: [(a -> a)] -> a -> a
compose = foldr (.) id
In a more explicit recursive form:
compose' [] = id
compose' (f:fs) = f . compose' fs
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