I find it difficult to understand the list

I just want a simple explanation. I have a whole bunch of functions, and one of my concepts in lists behaves very strangely.

sequence_ [writeArray arr (x, y) a | x <- xs, a <- as]
    where xs = [1,3,2]
    as = ["10", "8", "7"]

y is a constant (passed as an argument), and I cut out many other functions because they return what I expect.

Where i have an array that looks like

1,1,1
1,1,1
1,1,1

I expect to receive (for example)

1,1,10
1,1,8
1,1,7

But instead I get

1,1,7
1,1,7
1,1,7

Can anyone offer any advice?

+4
source share
1 answer

Enumerating lists by two variables does not iterate over two variables at the same time, but independently (you get all combinations of related values):

Prelude> [(x, y) | x <- [0..3], y <- [4..6]]
[(0,4),(0,5),(0,6),(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]

Thus, you perform the following operations:

sequence_ [writeArray arr (1, y) 10,
           writeArray arr (3, y) 10,
           writeArray arr (2, y) 10,
           writeArray arr (1, y) 8,
           writeArray arr (3, y) 8,
           writeArray arr (2, y) 8,
           writeArray arr (1, y) 7,
           writeArray arr (3, y) 7,
           writeArray arr (2, y) 7]

, 10, 8s, 7s.

luqui, :

sequence_ [writeArray arr (x, y) a | (x, a) <- (zip xs as)]
    where xs = [1,3,2]
    as = ["10", "8", "7"]
+6

All Articles