Post #2735427
2026-05-11 08:20 UTC
@byorgey@mathstodon.xyz Your way is a fold. There's also (of course!) an unfold:
```
transpose :: [[a]] -> [[a]]
transpose = unfoldr next where
next xss
| any null xss = Nothing
| otherwise = Just (map head xss, map tail xss)
```
That works for rectangular arrays. Coping also with upper left triangular ones needs a bit more work.
Replies (2)
-
@jer_gib@functional.cafe 2026-05-11 08:24
@byorgey@mathstodon.xyz Like this: ``` transpose :: [[a]] -> [[a]] transpose = unfoldr next where next xss = case takeWhile (not . null) xss of [] -> Nothing yss -> Just (map head yss, map tail yss) ```
-
@jer_gib@functional.cafe 2026-05-11 08:27
@byorgey@mathstodon.xyz What's more, `lzw` is another unfold. To be more precise, `uncurry (lzw f)` is an instance of `unfoldr`.