简体   繁体   中英

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)]'" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM