[Haskell-beginners] List comprehensions with multiple generators

Ken Overton ken.overton at gmail.com
Sun Apr 26 12:50:20 UTC 2020


Hello all,

I recently came across this function which made me realize I don't
understand list comprehensions well. I hope someone can help me understand
them better by understanding this example better. The function takes a list
of Eq and returns the list of unique elements from it:

    unique :: Eq a => [a] -> [a]
    unique xs = [x | (x,y) <- zip xs [0..], x `notElem` (take y xs)]

It's using a list comprehension with multiple 'generators' (hope I have the
term correctly). My understanding of multiple generators in a list
comprehension is that they refine the results of the previous generator.

So the first generator should produce [(Eq,Int)] as input to the second
generator? And the second generator should produce [Bool]?

My understanding must be wrong though; how do we end up with just the items
where the second generator produced True?

Thanks,


-- 
Ken Overton
(917) 863-3937
ken.overton at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200426/801702b5/attachment.html>


More information about the Beginners mailing list