Skip to content

Instantly share code, notes, and snippets.

@yamadapc
Last active September 17, 2015 15:35
Show Gist options
  • Save yamadapc/1ef00b0d73927df9eb64 to your computer and use it in GitHub Desktop.
Save yamadapc/1ef00b0d73927df9eb64 to your computer and use it in GitHub Desktop.
module CartesianProduct where
import Data.List (group)
cprod :: [a] -> [a] -> [(a, a)]
cprod xs ys = [ (x, y) | x <- xs, y <- ys ]
cprodAll :: Eq a => [[a]] -> [(a, a)]
cprodAll [] = []
cprodAll (x:xs) = map head $ group $ concat (map (cprod x) xs) ++ cprodAll xs
main :: IO ()
main = do
print $ cprodAll [[1, 2, 3], [1, 2], [3, 5], [1, 2], [8, 9, 10]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment