Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to take a list of functions and return a function which is their composite

Tags:

haskell

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.

like image 675
the_bamboo Avatar asked Feb 27 '17 19:02

the_bamboo


People also ask

How do you know if a function is composite?

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.

What is a real life example of a composite function?

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.

What is the use of composition of functions?

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.


1 Answers

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
like image 130
chepner Avatar answered Oct 15 '22 08:10

chepner