Splitting list into a list of possible tuples

后端 未结 6 811
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 04:59

I need to split a list into a list of all possible tuples, but I\'m unsure of how to do so.

For example:

pairs [\"cat\",\"dog\",\"mouse\"]

6条回答
  •  清酒与你
    2020-11-28 05:39

    My approach, which is somewhat similar to others'. It doesn't require Eq.

    allpairs :: [t] -> [(t,t)]
    allpairs [] = []
    allpairs [_] = []
    allpairs (x:xs) = concatMap (\y -> [(x,y),(y,x)]) xs ++ allpairs xs
    

提交回复
热议问题