module Utils.CartesianProduct
(
cartesianProduct,
cartesianPower,
(|*|),
(|^|)
)
where
import Data.List (replicate)
cartesianProduct :: [[a]] -> [[a]]
cartesianProduct :: forall a. [[a]] -> [[a]]
cartesianProduct [] = [[]]
cartesianProduct ([a]
x:[[a]]
xs) = [[[a]]] -> [[a]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ((\[a]
l -> [a
ea -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l | a
e <- [a]
x]) ([a] -> [[a]]) -> [[a]] -> [[[a]]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([[a]] -> [[a]]
forall a. [[a]] -> [[a]]
cartesianProduct [[a]]
xs))
(|*|) :: [a] -> [a] -> [[a]]
[a]
x |*| :: forall a. [a] -> [a] -> [[a]]
|*| [a]
y = [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
cartesianProduct [[a]
x,[a]
y]
cartesianPower :: [a] -> Int -> [[a]]
cartesianPower :: forall a. [a] -> Int -> [[a]]
cartesianPower [a]
l Int
k = [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
cartesianProduct ([[a]] -> [[a]]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [[a]]
forall a. Int -> a -> [a]
replicate Int
k [a]
l
|^| :: [a] -> Int -> [[a]]
(|^|) = [a] -> Int -> [[a]]
forall a. [a] -> Int -> [[a]]
cartesianPower