Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to take a specific value from a tuple list in haskell?

I have a function like this:

selectValue1 :: Int -> [(Int,Int)] -> [Int]
selectValue1 a [(x,y)]= [ y |(x,y)<-[(x,y)],x<-(x,y),x==a ]

what i want to do is to pass a tuple list to the function and take the second item in the tuple if the first item in the tuple matches with the input a.But this function give me an error:

Type error in generator
*** Term           : (x,y)
*** Type           : (Int,Int)
*** Does not match : [a]

why this happens??How can do the above task?? Any solutions?? Thank you all..:)

like image 320
Umesha Gunasinghe Avatar asked Jan 21 '23 06:01

Umesha Gunasinghe


1 Answers

selectValue1 a xs = [ y | (x,y) <- xs, x == a ]

First you shouldn't have pattern matched in the left hand side of the definition. You're just giving a name to your argument -- looking inside it, so to speak, can all happen in the list comprehension. Second, x <- (x,y) makes no sense. The rhs of the arrow in a list comprehension is always a list. In this case, it was doing nothing, so I removed it.

like image 179
sclv Avatar answered Jan 29 '23 10:01

sclv