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

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Cartesian product of two or more lists step by step in the Haskell 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 Haskell programming language

The provided Haskell code defines several functions and a main function that demonstrates their usage. A brief overview of each function:

  1. cartProd: This function takes two lists, xs and ys, and returns a list of all possible pairs of elements from the two lists, where each pair is represented as a tuple (x, y) where x is from xs and y is from ys. There are four different implementations of this function:

    • The first implementation uses list comprehensions and pattern matching to create the pairs.
    • The second implementation uses the >>= (bind) operator to flatten the nested lists.
    • The third implementation uses the (,) function to create tuples and fmap and (<*>) to apply these functions to the input lists.
    • The fourth implementation is a shorthand version of the third implementation.
  2. cartProdN: This function takes a list of lists and returns a list of all possible combinations of elements from the input lists. It has three implementations:

    • The first implementation uses sequence to combine the lists and then flatten the result.
    • The second implementation uses foldr and (<$>) to apply the (:) function to each element in the input lists.
    • The third implementation is a more concise version of the second implementation.
  3. main: The main function demonstrates the usage of the cartProd and cartProdN functions. It prints the results of applying these functions to various lists of integers.

Overall, the code showcases different ways to combine elements from multiple lists and generate all possible pairs or combinations of these elements.

Source code in the haskell programming language

cartProd :: [a] -> [b] -> [(a, b)]
cartProd xs ys =
  [ (x, y)
  | x <- xs 
  , y <- ys ]


cartProd :: [a] -> [b] -> [(a, b)]
cartProd xs ys = xs >>= \x -> ys >>= \y -> [(x, y)]


cartProd :: [a] -> [b] -> [(a, b)]
cartProd xs ys = (,) <$> xs <*> ys


cartProd :: [a] -> [b] -> [(a, b)]
cartProd = (<*>) . fmap (,)


main :: IO ()
main =
  mapM_ print $
  uncurry cartProd <$>
  [([1, 2], [3, 4]), ([3, 4], [1, 2]), ([1, 2], []), ([], [1, 2])]


cartProdN :: [[a]] -> [[a]]
cartProdN = sequence

main :: IO ()
main = print $ cartProdN [[1, 2], [3, 4], [5, 6]]


cartProdN :: [[a]] -> [[a]]
cartProdN = foldr (\xs as -> xs >>= (<$> as) . (:)) [[]]


cartProdN :: [[a]] -> [[a]]
cartProdN = foldr
    (\xs as ->
        [ x : a
        | x <- xs
        , a <- as ])
    [[]]


main :: IO ()
main = do
  mapM_ print $ 
    cartProdN [[1776, 1789], [7,12], [4, 14, 23], [0,1]]
  putStrLn ""
  print $ cartProdN [[1,2,3], [30], [500, 100]]
  putStrLn ""
  print $ cartProdN [[1,2,3], [], [500, 100]]


  

You may also check:How to resolve the algorithm Binary digits step by step in the Peloton programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Scratch programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the ML programming language