在三元组中生成所有可能的数字组合? [英] Generating all possible combinations of numbers in a triplet?
本文介绍了在三元组中生成所有可能的数字组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[(0,0,0),(0,0,1),(0,1,1) ,(1,1,1),(0,0,2),(0,1,2),(0,2,2),(1,2,2)]
$ c $ (例如:100:给我们一个最后的三重数(100,100,100));等等,等等。是否有任何合理的方式在haskell中做这件事,或者我最好写一个简单地持有边界指针的方法,递归地增加每个数字,直到它等于它的右边的数字?解决方案我认为您的描述最符合列表理解来表达您想要做的事情:
[(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屋!
查看全文