开发者

Problem in List of tuples

开发者 https://www.devze.com 2023-03-11 04:57 出处:网络
I have a list of tuples which i need to return a [Int] which are all the locations开发者_如何学Go are dividable by 2 ..

I have a list of tuples which i need to return a [Int] which are all the locations开发者_如何学Go are dividable by 2 ..

type A = [(Int, Int, Int, Int)]

func :: A -> [Int]
func tuples =  [a | (a, b, c, d) <- tuples, map a `mod` 2 == 0]

func [(244,244,244,244),(244,244,244,244),(244,244,244,244)]

Output

[244,244,244]

I have the current code but problem is this only checking position of a .. but i required to all a ,b , c,d ?


type A = (Int, Int, Int, Int)
func :: [A] -> [Int]
func t =  [a | (a, b, c, d) <- t, all even [a,b,c,d]]

The all function returns true only if everything given satisfies the predicate. I've bundled the tuple into a list and checked the predicate.


Just add some more guards for b, c and d:

a `divides` b =  b `mod` a == 0
func tuples =  [a | (a, b, c, d) <- tuples, all (divides 2) [a,b,c,d]]
0

精彩评论

暂无评论...
验证码 换一张
取 消