-1

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.

15
  • 2
    Infinite tuples are not possible in Haskell, that would be something like type tup = (X, tup). GHC rejects such infinite types. [[n]] is the type of a list, which can indeed be infinite (but only at runtime; it's perfectly finite to the type checker). Commented Jun 21, 2016 at 16:24
  • 1
    Not really; you need dependent types to specify that the return value is a list of lists of length 2, etc. Even if you could, Num is an overly broad constraint; you can't have a list of length 1.5 (or a 1.5-tuple). Commented Jun 21, 2016 at 16:32
  • 2
    I think it is clear from the provided example that the OP wants to generate an infinite list of lists - not tuples. Commented Jun 21, 2016 at 16:38
  • 5
    You want a list-of-lists. There are no tuples in the type n -> [[n]]. Also, Int n => ... is not Haskell, unless Int is a custom class -- was that Num n => ... instead? Commented Jun 21, 2016 at 16:38
  • 2
    so generate k = concatMap (allTuplesWithMaxElem k) [0..]? Commented Jun 21, 2016 at 17:46

1 Answer 1

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] ?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.