haskell - apply a list of functions to a value one after another -
this question has answer here:
is there built in function apply list of functions value 1 after another? using this, seems quite inelegant me.
-- applyfunctions [(*2), (+3), (*4)] 1 == ((1 * 2) + 3 ) * 4 applyfunctions :: [(a -> a)] -> -> applyfunctions [] x = x applyfunctions [f] x = f x applyfunctions (f:fs) x = applyfunctions fs (f x)
you can use foldl (flip ($))
foldl (flip ($)) 1 [(*2), (*3), (*4)] --yields 24
Comments
Post a Comment