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 642717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:12:13+00:00 2026-05-13T21:12:13+00:00

I am curious if it is possible to dynamically build a list comprehension in

  • 0

I am curious if it is possible to dynamically build a list comprehension in Haskell.

As an example, if I have the following:

all_pows (a,a') (b,b') = [ a^y * b^z | y <- take a' [0..], z <- take b' [0..] ]

I get what I am after

*Main> List.sort $ all_pows (2,3) (5,3)
[1,2,4,5,10,20,25,50,100]

However, what I’d really like is to have something like

all_pows [(Int,Int)] -> [Integer]

So that I can support N pairs of arguments without building N versions of all_pows. I’m still pretty new to Haskell so I may have overlooked something obvious. Is this even possible?

  • 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-05-13T21:12:13+00:00Added an answer on May 13, 2026 at 9:12 pm

    The magic of the list monad:

    ghci> let powers (a, b) = [a ^ n | n <- [0 .. b-1]]
    ghci> powers (2, 3)
    [1,2,4]
    ghci> map powers [(2, 3), (5, 3)]
    [[1,2,4],[1,5,25]]
    ghci> sequence it
    [[1,1],[1,5],[1,25],[2,1],[2,5],[2,25],[4,1],[4,5],[4,25]]
    ghci> mapM powers [(2, 3), (5, 3)]
    [[1,1],[1,5],[1,25],[2,1],[2,5],[2,25],[4,1],[4,5],[4,25]]
    ghci> map product it
    [1,5,25,2,10,50,4,20,100]
    ghci> let allPowers list = map product $ mapM powers list
    ghci> allPowers [(2, 3), (5, 3)]
    [1,5,25,2,10,50,4,20,100]
    

    This probably deserves a bit more explanation.

    You could have written your own

    cartesianProduct :: [[a]] -> [[a]]
    cartesianProduct [] = [[]]
    cartesianProduct (list:lists)
      = [ (x:xs) | x <- list, xs <- cartesianProduct lists ]
    

    such that cartesianProduct [[1],[2,3],[4,5,6]] ⇒ [[1,2,4],[1,2,5],[1,2,6],[1,3,4],[1,3,5],[1,3,6]].

    However, comprehensions and monads are intentionally similar. The standard Prelude has sequence :: Monad m => [m a] -> m [a], and when m is the list monad [], it actually does exactly what we wrote above.

    As another shortcut, mapM :: Monad m => (a -> m b) -> [a] -> m [b] is simply a composition of sequence and map.

    For each inner list of varying powers of each base, you want to multiply them to a single number. You could write this recursively

    product list = product' 1 list
      where product' accum [] = accum
            product' accum (x:xs)
              = let accum' = accum * x
                 in accum' `seq` product' accum' xs
    

    or using a fold

    import Data.List
    product list = foldl' (*) 1 list
    

    but actually, product :: Num a => [a] -> a is already defined! I love this language ☺☺☺

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

Sidebar

Related Questions

curious if anyone might have some insight in how I would do the following
Curious to get people's thoughts. I conduct frequent interviews, and have had enough in
Just curious here: is it possible to invoke a Windows Blue Screen of Death
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template
I curious to how different people solve integration of systems. I have a feeling
Curious to know how people set up their personal and/or work development environment, in
Curious as to 99.95% uptime REALLY means; Is it really going to go down
Im curious what the exact meaning of committed memory is when the value is
Just curious: 4 instanceof Number => false new Number(4) instanceof Number => true? Why
I'm curious to hear the experiences of those who are currently running their SVN

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.