Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm having a hard time with a list comprehension

I just want a simple explanation, really. I have a whole bunch of functions and one of my list comprehensions is behaving very oddly.

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

y is constant (passed in as an argument) and I've cut out a lot of the other functions because they are returning what I am expecting.

Where I have an array that looks like

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

I expect to get (for example)

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

But instead I get

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

Is anyone able to offer any advice?

like image 491
Thanasi Poulos Avatar asked Mar 16 '23 16:03

Thanasi Poulos


1 Answers

List comprehensions over two variables do not iterate over the two variables simultaneously, but independently (you get all combinations of bound 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)]

You are therefore sequencing 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]

So you're writing all 10s, overwriting them with 8s, and then overwriting them again with 7s.

As luqui suggested in a comment, try this instead:

sequence_ [writeArray arr (x, y) a | (x, a) <- (zip xs as)]
    where xs = [1,3,2]
    as = ["10", "8", "7"]
like image 190
Neil Forrester Avatar answered Mar 23 '23 21:03

Neil Forrester