Haskell generate list of lists

感情迁移 提交于 2019-12-13 09:37:26

问题


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.


回答1:


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] ?


来源:https://stackoverflow.com/questions/37949703/haskell-generate-list-of-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!