Вопрос

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]])

Это было полезно?

Решение

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 ...

Другие советы

In your case it is not a list, but actually a tuple. Tuples are heterogenous containers, so that is why different types may be inside them. However, swapping elements in tuple is impossible to be written in generic way, at least with standard Haskell. I suggest operating on data pointed by Bryan.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top