Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:08:55+00:00 2026-06-18T11:08:55+00:00

I am very bad at wording things, so please bear with me. I am

  • 0

I am very bad at wording things, so please bear with me.

I am doing a problem that requires me to generate all possible numbers in the form of a lists of lists, in Haskell.

For example if I have x = 3 and y = 2, I have to generate a list of lists like this:

[[1,1,1], [1,2,1], [2,1,1], [2,2,1], [1,1,2], [1,2,2], [2,1,2], [2,2,2]]

x and y are passed into the function and it has to work with any nonzero positive integers x and y.

I am completely lost and have no idea how to even begin.

For anyone kind enough to help me, please try to keep any math-heavy explanations as easy to understand as possible. I am really not good at math.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:08:57+00:00Added an answer on June 18, 2026 at 11:08 am

    Assuming that this is homework, I’ll give you the part of the answer, and show you how I think through this sort of problem. It’s helpful to experiment in GHCi, and build up the pieces we need. One thing we need is to be able to generate a list of numbers from 1 through y. Suppose y is 7. Then:

    λ> [1..7]
    [1,2,3,4,5,6,7]
    

    But as you’ll see in a moment, what we really need is not a simple list, but a list of lists that we can build on. Like this:

    λ> map (:[]) [1..7]
    [[1],[2],[3],[4],[5],[6],[7]]
    

    This basically says to take each element in the array, and prepend it to the empty list []. So now we can write a function to do this for us.

    makeListOfLists y = map (:[]) [1..y]
    

    Next, we need a way to prepend a new element to every element in a list of lists. Something like this:

    λ> map (99:) [[1],[2],[3],[4],[5],[6],[7]]
    [[99,1],[99,2],[99,3],[99,4],[99,5],[99,6],[99,7]]
    

    (I used 99 here instead of, say, 1, so that you can easily see where the numbers come from.) So we could write a function to do that:

    prepend x yss = map (x:) yss
    

    Ultimately, we want to be able to take a list and a list of lists, and invoke prepend on every element in the list to every element in the list of lists. We can do that using the map function again. But as it turns out, it will be a little easier to do that if we switch the order of the arguments to prepend, like this:

    prepend2 yss x = map (x:) yss
    

    Then we can do something like this:

    λ>  map (prepend2 [[1],[2],[3],[4],[5],[6],[7]]) [97,98,99]
    [[[97,1],[97,2],[97,3],[97,4],[97,5],[97,6],[97,7]],[[98,1],[98,2],[98,3],[98,4],[98,5],[98,6],[98,7]],[[99,1],[99,2],[99,3],[99,4],[99,5],[99,6],[99,7]]]
    

    So now we can write that function:

    supermap xs yss = map (prepend2 yss) xs
    

    Using your example, if x=2 and y=3, then the answer we need is:

    λ> let yss = makeListOfLists 3
    λ> supermap [1..3] yss
    [[[1,1],[1,2],[1,3]],[[2,1],[2,2],[2,3]],[[3,1],[3,2],[3,3]]]
    

    (If that was all we needed, we could have done this more easily using a list comprehension. But since we need to be able to do this for an arbitrary x, a list comprehension won’t work.)

    Hopefully you can take it from here, and extend it to arbitrary x.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is locking a type considered very bad? For example, lock(typeof(DateTime)) I understand that
Is there a "very bad thing" that can happen &&= and ||= were used
Someone told me that openGL is for graphic only, and that it's very bad
First of all, sorry the way I write, bad English here. Please, consider the
I operate an online browser game that is very AJAX/database dependent, and the problem
I have the following SQL statement which has a very bad performance: SELECT frmInstLastModifiedDate
I am very new to Entity Framework, so apologies if its a very bad
Today I got this question for which I think I answered very bad. I
I feel bad because it is very likely this has already been answered and
A very basic question.. but I always do the bad implementation of this. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.