user12117801
user12117801

Reputation:

Apply a list of functions to an input

I'd like to apply a list of functions to an input. For example, if my list fo functions is [(+1),(*3),(+(-2))] and my input is 5, I would like to apply the functions from right to left, so the output would be 10.

Upvotes: 2

Views: 236

Answers (1)

user12117801
user12117801

Reputation:

Solved with mkUltras' comment:

You can use foldr or foldl to reduct a list depends on the direction. To apply function user $. In this case expression is: foldr ($) 5 [(+1),(*3),(+(-2))]

My final code: succApply x n = foldr ($) n x

Example inputs:

succApply [(+1),(*2),(+(-1))] 1 == 1
succApply [init, tail] [1..5] == [2,3,4]
succApply [(*2), id, (+1)] 5 == 12

Upvotes: 2

Related Questions