Haskell生成列表的列表 [英] Haskell generate list of lists

查看:135
本文介绍了Haskell生成列表的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

其中list是n元组,n是来自参数。



此外,这些清单必须进行排序。



例如:

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

我只能使用基本功能。



我知道它确实很简单,但我不知道如何做到这一点,我也无法在互联网上找到它。



我会很乐意提供任何帮助。

解决方案

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



假设您想从数字 [0..2] 中生成所有列表 [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] ?

这篇关于Haskell生成列表的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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