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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:30:06+00:00 2026-05-14T08:30:06+00:00

I have a problem with writing a Cartesian power function. I found many examples

  • 0

I have a problem with writing a Cartesian power function. I found many examples about calculating Cartesian Product, but no one about Cartesian power.
For example, [1;2] raised to power 3 = [ [1;1;1] ; [1;1;2] ; [1;2;1] ; [1;2;2] ; [2;1;1] ; [2;1;2] ; [2;2;1]; [2;2;2] ]
I use following code to calculate Cartesian Product:

 let Cprod U V =
        let mutable res = []
        for u in U do
            for v in V do
                res <- res @ [[u;v]]
        res

And trying to calculate Cartesian power.
I use following code to calculate Cartesian Product:

let Cpower U n =
    let mutable V = U
    for i=0 to n-1 do
        V <- Dprod U V
    V

Visual Studio said: Error The resulting type would be infinite when unifying ”a’ and ”a list’. I will thankful for any help and links.

  • 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-14T08:30:06+00:00Added an answer on May 14, 2026 at 8:30 am

    I would also add that it is generally prefered to avoid using mutable values when writing F# code. It’s fine when you’re learning F# or when you need to optimize some code to run faster, but if you want to write a more idiomatic F# code, it’s better to use recursion instead of mutable values.

    I tried to write the Cartesian power a bit more elegantly and here is my version. It is implemented recursively. I explicitly handle the case when we need to calculate X^1 and the recursive case performs a Cartesian product like this: X^n = X * X^(n-1)

    I’m using sequence expressions and the method generates elements of the sequence (to be returned as the result) using yield:

    let rec cartesianPow input n = seq {
      if (n = 1) then
        // This handles the case when the recursion terminates. We need to turn
        // each element from the input into a list containing single element:
        //   [1; 2; 4] ^ 1 = [ [1]; [2]; [3] ]
        for el in input do 
          yield [el]
      else
        // We perform one Cartesian product (and run the rest of the 
        // power calculation recursively). Mathematically:
        //   [1; 2; 3] ^ n = [1; 2; 3] x ([1; 2; 3] ^ (n-1))
        for el in input do 
          for rest in cartesianPow input (n - 1) do
            yield el :: rest }
    
    cartesianPow [ 0; 1 ] 3
    

    This isn’t the most efficient implementation (e.g. because using yield inside for loop may not be a good thing to do), but that would be only problem for large n. In F#, it is usually a good idea to start with the cleanest implementation that’s easier to understand :-).

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

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • 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 The current version (1.9) of MAMP / MAMP PRO includes… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer Output Buffering for Web Developers, a Beginner’s Guide: Without output… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer When it comes to managing histories and providing the functionality… May 14, 2026 at 9:37 pm

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.