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

  • Home
  • SEARCH
  • 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
  • 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. 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

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Please see: FIX: You may experience problems when you use… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer When you reload/refresh a web page most of the time… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer It works this way: select * from ( select top… May 11, 2026 at 4:44 pm

Related Questions

(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
I'm importing data from a CSV that contains approx 350 columns. This CSV import
I am trying to write a batch script and trying to wait 10 seconds
Problem: There are a bunch of .lnk files on the C drive that point

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.