How to resolve the algorithm Cartesian product of two or more lists step by step in the Bracmat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cartesian product of two or more lists step by step in the Bracmat programming language

Table of Contents

Problem Statement

Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: and, in contrast: Also demonstrate, using your function/method, that the product of an empty list with any other list is empty. For extra credit, show or write a function returning the n-ary product of an arbitrary number of lists, each of arbitrary length. Your function might, for example, accept a single argument which is itself a list of lists, and return the n-ary product of those lists. Use your n-ary Cartesian product function to show the following products:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cartesian product of two or more lists step by step in the Bracmat programming language

Source code in the bracmat programming language

( ( mul
  =   R a b A B
    .   :?R
      & !arg:(.?A) (.?B)
      & (   !A
          :   ?
              ( %@?a
              &   !B
                :   ?
                    ( (%@?b|(.?b))
                    & !R (.!a !b):?R
                    & ~
                    )
                    ?
              )
              ?
        | (.!R)
        )
  )
& ( cartprod
  =   a
    .   !arg:%?a %?arg&mul$(!a cartprod$!arg)
      | !arg
  )
&   out
  $ ( cartprod
    $ ( (.1776 1789)
        (.7 12)
        (.4 14 23)
        (.0 1)
      )
    )
& out$(cartprod$((.1 2 3) (.30) (.500 100)))
& out$(cartprod$((.1 2 3) (.) (.500 100)))
)

  

You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Python programming language
You may also check:How to resolve the algorithm Even or odd step by step in the L++ programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the Raku programming language