Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert `[[Either String c]]` into `Either String [[c]]`?

Tags:

haskell

Say I have two functions:

b2c :: B -> Either String C
a2bs :: A -> [[B]]

How to make the following a2cs function using b2c and a2bs such that if there is any Left value in the [[Either String c]] then Either String [[c]] should be a Left value?

a2cs :: A -> Either String [[C]]
like image 431
Leo Zhang Avatar asked Jun 19 '26 17:06

Leo Zhang


1 Answers

I would write this:

a2cs = traverse (traverse b2c) . a2bs

Although this doesn't use exactly your proposed implementation strategy of first producing a [[Either String C]] and then flattening that to Either String [[C]], it has the advantage that it requires only one pass over the nested lists rather than the two required by your strategy.

like image 82
Daniel Wagner Avatar answered Jun 21 '26 10:06

Daniel Wagner