Compose Functions

Computer Science Level pending

Consider a higher order function applyAll that takes in a list of functions and a value and applies all the functions sequentially on the value.

For example,

applyAll [f,g,h] x = f (g (h x))

Which of the following higher order functions would be useful to define such an applyAll ?

map fold zip filter

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Notice that we are attempting to create a function by composing all the functions.

Hence, we could define this with fold

1
2
applyAll :: [(a -> a)] -> a -> a
applyAll fs x = (foldr (.) id fs)  x

Or simply,

1
applyAll = foldr (.) id 

You could use foldr1 directly without the use of the identity function.

1
applyAll = foldr1 (.) 

A Former Brilliant Member - 4 years, 9 months ago

Log in to reply

Interesting, what does foldr1 do?

Agnishom Chattopadhyay - 4 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...