Accumulating and threading output of functions

Hello all, I have a number of functions, whose type is something like this: f:: a -> b -> (b, c) i.e. it returns a modified b and an additional c. Now I want to write a combinator, which assembles a list of such functions into a single one. This combinator will have to thread the bs from one function to the next and accumulate the cs into a single c. While I was able to write this using a fold, I am not happy with it (my first attempt had a bad error in it, which is always a bad sign). Are you aware of any idioms, how to do such things on the beaten track?

If c is a Monoid, you can use the RWS a c b () type instead and combine
them with sequence_.
On 4 Jun 2016 5:19 pm, "martin"
Hello all,
I have a number of functions, whose type is something like this:
f:: a -> b -> (b, c)
i.e. it returns a modified b and an additional c.
Now I want to write a combinator, which assembles a list of such functions into a single one. This combinator will have to thread the bs from one function to the next and accumulate the cs into a single c.
While I was able to write this using a fold, I am not happy with it (my first attempt had a bad error in it, which is always a bad sign).
Are you aware of any idioms, how to do such things on the beaten track?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

First of all: where does your first argument (`a`) come from? Is it the same for all of those functions? If so, you can get rid of it by using `map ($ a)`. Now, you have a list of functions of type `b -> (b, c)`. The most natural thing here is to convert them to the State monad: `State b c`. And your original functions would be `a -> State b c`. Try to use State monad from the start. Then, from the list of type `[State b c]`, the `sequence` function gives you `State b [c]` instead. `evalState` would give you just `[c]`, and you can convert the list of `c`s into a single `c` in any way you want.
On 04 Jun 2016, at 17:18, martin
wrote: Hello all,
I have a number of functions, whose type is something like this:
f:: a -> b -> (b, c)
i.e. it returns a modified b and an additional c.
Now I want to write a combinator, which assembles a list of such functions into a single one. This combinator will have to thread the bs from one function to the next and accumulate the cs into a single c.
While I was able to write this using a fold, I am not happy with it (my first attempt had a bad error in it, which is always a bad sign).
Are you aware of any idioms, how to do such things on the beaten track?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (4)
-
martin
-
MigMit
-
Patrick Chilton
-
Stefan Monnier