Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the name of the function that applies each function in a list of functions to a value progressively, accumulating the results?

Tags:

haskell

It's like the title says. I've implemented this, but maybe this function already has a common name and exists in a standard library.

Other suggestions about this function are welcome. Maybe there is a cleaner implementation.

let transform x funcList = transform' x [x] funcList
        where transform' startVal accum funcList 
                   | null funcList = reverse accum
                   | otherwise = let result = (head funcList) startVal
                                     in transform' result (result:accum) $ tail funcList

When executed, it does this:

> transform 2 [(2 + ),((-1) +),(3 *)]
[2,4,3,9]
like image 935
renick Avatar asked Jan 17 '26 14:01

renick


1 Answers

You can define it with scanl:

let transform = scanl (\v f -> f v)

or

let transform = scanl (flip ($))
like image 180
Lee Avatar answered Jan 20 '26 22:01

Lee