且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Haskell生成列表的列表

更新时间:2023-11-25 23:35:28

以下是 allTuplesWithMaxElem 函数的提示...



假设您想从数字 [0..2] $ c $>中生成所有列表 [a,b,c,d] 2>必须出现作为其中一个元素的限制。



考虑以下问题:

 如果我们设置a = 0,[b,c,d]有什么可能性? 
如果我们设定a = 1,那么[b,c,d]有什么可能性?
如果我们设定a = 2,[b,c,d]有什么可能性?


I am preparing for test in haskell and I found a task from last test where was given to generate infinite list lists.

generate :: Num n => n -> [[n]]

where list is n-tuple, n is from parameter.

moreover the lists has to be sorted.

For exaple:

generate 2 = [[0,0],[0,1],[1,0],[1,1],[0,2],[1,2],[2,0],[2,1],[2,2],[0,3],....]

I have to use only basic functions.

I know that it surely is simple, but I dont know how to do it and I also can't find it on the internet.

I will be really glad for any help.

Here's a hint for the allTuplesWithMaxElem function...

Suppose you want to generate all lists [a,b,c,d] from the numbers [0..2] with the restriction that 2 must appear as one of the elements.

Consider these questions:

If we set a = 0, what are the possibilities for [b,c,d] ?
If we set a = 1, what are the possibilities for [b,c,d] ?
If we set a = 2, what are the possibilities for [b,c,d] ?