
Hi, The lookup function can locate something in a a-list: lookup :: Eq a => a -> [(a,b)] -> Maybe b is there an easy way to do a reverse lookup: rlookup :: Eq b => b -> [(a,b)] -> Maybe a or perhaps a builtin function to flip a pair: (a, b) -> (b, a) Thanks, Patrick -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Patrick LeBoutillier wrote:
is there... perhaps a builtin function to flip a pair: (a, b) -> (b, a)
The function swap (x, y) = (y, x) will be added to Data.Tuple in the next release of the base libraries. In the meantime, it is easy enough to define it yourself, and then, of course rlookup x = lookup x . map swap Or, you could use the find function from Data.List and write: rlookup x = find ((== x) . snd) Regards, Yitz

Incidentally (and more as a curiosity than anything else)
Prelude> :t uncurry . flip . curry $ id
uncurry . flip . curry $ id :: (b, a) -> (a, b)
On Wed, Jul 7, 2010 at 1:54 PM, Yitzchak Gale
Patrick LeBoutillier wrote:
is there... perhaps a builtin function to flip a pair: (a, b) -> (b, a)
The function
swap (x, y) = (y, x)
will be added to Data.Tuple in the next release of the base libraries. In the meantime, it is easy enough to define it yourself, and then, of course
rlookup x = lookup x . map swap
Or, you could use the find function from Data.List and write:
rlookup x = find ((== x) . snd)
Regards, Yitz _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Alex R
participants (3)
-
Alex Rozenshteyn
-
Patrick LeBoutillier
-
Yitzchak Gale