Search code examples
haskellnested-listshotswap

swap nested list in haskell


I want to swap a list like (1,3,[2,4],5,[7,12,[14]])

I know that in haskell types of a list should be the same so I confused how can I do this.

Should I use another data structure? or I should define another data type?

for example swap 2 3 (1,3,[2,4],5,[7,12,[14]]) ==> (1,3,5,[2,4],[7,12,[14]])


Solution

  • Indeed such lists are not possible and it really is a tree you are looking at, for example something like this.

    data NestedList a = Leaf a | Node [NestedList a]
    

    Now you have to think what you really mean by swap. You can swap the top level lists, you can swap leaves and ...