将列表拆分为可能的元组列表 [英] Splitting list into a list of possible tuples

查看:23
本文介绍了将列表拆分为可能的元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个列表拆分为一个包含所有可能元组的列表,但我不确定如何进行.

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

例如:

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

应该导致:

[("cat","dog"), ("cat","mouse"), ("dog","cat"), ("dog","mouse"), ("mouse")","cat"), ("mouse","dog")]

我能够形成前两个,但不确定如何获得其余的.

I was able to form the first two, but am unsure of how to get the rest.

这是我目前所拥有的:

pairs :: [a] -> [(a,a)]
pairs (x:xs) = [(m,n) | m <- [x], n <- xs]

推荐答案

您可以使用列表推导式:

You can use a list comprehension:

allpairs :: Eq a => [a] -> [(a,a)]
allpairs xs = [ (x1,x2) | x1 <- xs, x2 <- xs, x1 /= x2 ]

这篇关于将列表拆分为可能的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆