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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:52:23+00:00 2026-05-10T19:52:23+00:00

Note This is not a REBOL-specific question. You can answer it in any language.

  • 0

Note

This is not a REBOL-specific question. You can answer it in any language.

Background

The REBOL language supports the creation of domain-specific languages known as ‘dialects’ in REBOL parlance. I’ve created such a dialect for list comprehensions, which aren’t natively supported in REBOL.

A good cartesian product algorithm is needed for list comprehensions.

The Problem

I’ve used meta-programming to solve this, by dynamically creating and then executing a sequence of nested foreach statements. It works beautifully. However, because it’s dynamic, the code is not very readable. REBOL doesn’t do recursion well. It rapidly runs out of stack space and crashes. So a recursive solution is out of the question.

In sum, I want to replace my meta-programming with a readable, non-recursive, ‘inline’ algorithm, if possible. The solution can be in any language, as long as I can reproduce it in REBOL. (I can read just about any programming language: C#, C, C++, Perl, Oz, Haskell, Erlang, whatever.)

I should stress that this algorithm needs to support an arbitrary number of sets to be ‘joined’, since list comprehension can involve any number of sets.

  • 1 1 Answer
  • 3 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. 2026-05-10T19:52:24+00:00Added an answer on May 10, 2026 at 7:52 pm

    How about something like this:

    #!/usr/bin/perl  use strict; use warnings;  my @list1 = qw(1 2); my @list2 = qw(3 4); my @list3 = qw(5 6);  # Calculate the Cartesian Product my @cp = cart_prod(\@list1, \@list2, \@list3);  # Print the result foreach my $elem (@cp) {   print join(' ', @$elem), '\n'; }  sub cart_prod {   my @sets = @_;   my @result;   my $result_elems = 1;    # Calculate the number of elements needed in the result   map { $result_elems *= scalar @$_ } @sets;   return undef if $result_elems == 0;    # Go through each set and add the appropriate element   # to each element of the result   my $scale_factor = $result_elems;   foreach my $set (@sets)   {     my $set_elems = scalar @$set;  # Elements in this set     $scale_factor /= $set_elems;     foreach my $i (0 .. $result_elems - 1) {       # Calculate the set element to place in this position       # of the result set.       my $pos = $i / $scale_factor % $set_elems;       push @{$result[$i]}, $$set[ $pos ];     }   }    return @result; } 

    Which produces the following output:

    1 3 5 1 3 6 1 4 5 1 4 6 2 3 5 2 3 6 2 4 5 2 4 6 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Moderator note: This question is not a good fit for our question and answer
(Note: This is not a question about what is the best way with code
Please note this is not a question about online/hosted SVN services. I am working
(note that this question is not about CAS, it's about the May fail spuriously
NOTE: sorry as this is not a programming question but i am not aware
Please note this is a question with regard to Backbone.js. This is not a
This question is not necessarily Couchbase 2.0 develop preview specific however I think it
NOTE: This is not the same question as Python: Persistent shell variables in subprocess
(Note: This is not a question of how to flush a write() . This
NOTE This was flagged as a potentially subjective question, but it is not subjective.

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.