Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - List comprehension in a list of tuples (a,[a])

So, let's go to the point:

f :: Eq a => a -> [(a,[a])] -> [(a,[a])]
f x list = [(a,[m]) | (a,[m]) <- list, x == a]

so:

   f 1 [(1,[1])] = [(1,[1])]

but

  f 1 [(1,[1,1])] = []

Why is that?? Thank you!

(I'm a newbie, so I apologize if it's a too dumb question, but I really can't find an explanation)

like image 309
dehq Avatar asked Dec 27 '12 22:12

dehq


Video Answer


1 Answers

That's because [1,1] does not match the pattern [m]. The latter means a list of one item (unless used where a type name should be). Haskell already knows that second element of the pair is a list, (by looking at the type of list parameter) and does not need to be told that explicitly.

like image 121
Karolis Juodelė Avatar answered Sep 23 '22 06:09

Karolis Juodelė