在三元组中生成所有可能的数字组合? [英] Generating all possible combinations of numbers in a triplet?

查看:187
本文介绍了在三元组中生成所有可能的数字组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举个例子,我想要构造一个三元组,在三元组中取1到100之间的数字的每个组合;即:

  [(0,0,0),(0,0,1),(0,1,1) ,(1,1,1),(0,0,2),(0,1,2),(0,2,2),(1,2,2)] 
解决方案

我认为您的描述最符合列表理解来表达您想要做的事情:

  [(a,b,c)| c < -  [0..100],
b < - [0..c],
a < - [0..b]]


Say for example I want to construct a triplet, taking in every combination of numbers from 1..100 in a triplet; i.e:

[(0,0,0),(0,0,1),(0,1,1),(1,1,1),(0,0,2),(0,1,2),(0,2,2),(1,2,2)]

..etc etc, up until a given bound (i.e: 100: giving us a final triplet of (100,100,100)); is there any reasonable way of doing this within haskell, or would I be best off writing a method that in short held a boundary pointer, and recursively increased each number until it was equal to the number to its right?

解决方案

I think your description best fits a list comprehension to express what you want to do:

[(a, b, c) | c <- [0..100],
             b <- [0..c],
             a <- [0..b] ]

这篇关于在三元组中生成所有可能的数字组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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