Calculate n-ary Cartesian Product

后端 未结 6 2135
别跟我提以往
别跟我提以往 2020-12-16 18:03

Given two lists, I can produce a list of all permutations the Cartesian Product of these two lists:

permute :: [a] -> [a] -> [[a]]
per         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 18:24

    Here is my way of implementing it simply, using only list comprehensions.

    crossProduct :: [[a]] -> [[a]]
    crossProduct (axis:[]) = [ [v] | v <- axis ]
    crossProduct (axis:rest) = [ v:r | v <- axis, r <- crossProduct rest ]
    

提交回复
热议问题