Is it possible to reorganize nested tuples?

后端 未结 2 1797
旧巷少年郎
旧巷少年郎 2021-02-10 22:42

What I want to do is something like this:

Take an arbitrary polymorphic tuple:

x = (((1, \"\"), Nothing), (\'\', 6))

And reorganize wit

2条回答
  •  野性不改
    2021-02-10 23:05

    I'm still recent to Haskell but I'd do this with a pattern-matching function.

    converter :: (((Int, String), Maybe a), (Char, Int)) -> (Int, (Char, Maybe Int, (String, (Int, ()))))
    converter (((i1, s), m), (c, i2)) = (i1, (c, (m, (s, (i2, ())))))
    

    You of course could replace all the concrete types with type variables and it would also work.

    converter :: (((a, b), c), (d, e)) -> (a, (d, c, (b, (e, ()))))
    converter (((i1, s), m), (c, i2)) = (i1, (c, (m, (s, (i2, ())))))
    

    (Obviously you'd want to get the types in the correct order and make sure this all compiles.)

提交回复
热议问题