How to expand
concat( map (interleave 2) [[3,4][4,3]])
interleave::a->[a]->[[a]]
interleave x []=[[x]]
interleave x (y:ys)=(x:y:ys):map (y:) (interleave x ys)
I used ghci interpreter and got the following answer:
[[2,3,4],[4,2,3],[3,4,2],[2,4,3],[3,2,4],[4,3,2]]
I run the following code :
map (interleave 2) [[3,4][4,3]]
and got the output :
[[[2,3,4],[4,2,3],[3,4,2]],[[2,4,3],[3,2,4],[4,3,2]]]
When I do dry run I did the following:
concat( map (interleave 2) [[3,4][4,3]])
=>
concat( interleave 2 [3,4], interleave 2 [4,3])
=>
concat([[2,3,4],[4,2,3],[3,4,2]],[[2,4,3],[3,2,4],[4,3,2]])
=>
[2,3,4,4,2,3,3,4,2,2,4,3,3,4,2,4,3,2]
Can someone please explain how the expression evaluated?