What you want to check is: for every tuple (x,y)
in the list, (y,x)
should also be present. You can express that quite directly in Haskell:
isSymmetric :: Eq a => [(a,a)] -> Bool
isSymmetric l = all ((x,y) -> (y,x)`elem`l) l
This is actually doing some redundant work because it always also goes over (x,y)
itself, which your not really interested in, but it doesn't really matter. However it's a good exercise to design this in a way so it doesn't go over the element itself; for this it's helpful to use an auxiliary function
foci :: [a] -> [(a,[a])]
witht the behaviour
foci [p,q,r] ≡ [(p,[q,r]), (q,[p,r]), (r,[p,q])]
Then you left with an all
over the foci
of the input list, i.e.
isSymmetric = all _ . foci
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…