plshelp
plshelp

Reputation: 45

List Comprehensions with a condition on an array?

I have an array that contains tuples that look like this: [((Int, Int), Int)]. I am trying to create a new array with list comprehensions that says to only add all 3 Ints if the 3rd Int == a certain number. I have it written as

newArray = [((x,y),z) | ((x,y),z)<-oldArray, (snd oldArray) == 5]

However, when I try to run the code that contains this, it says "couldn't match expected type" and point the error at my conditional. "Couldn't match expected type '(a0, Int)' with actual type '[((Int, Int), Int)]'".

Upvotes: 0

Views: 163

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153102

oldArray is a list, always and forever, even inside the comprehension, so snd can't be applied to it. Use z == 5 instead.

Upvotes: 3

Related Questions