Find all combinations of a given set of numbers

前端 未结 9 1645
故里飘歌
故里飘歌 2020-12-03 09:30

say I have a set of numbers \'0\', \'1\', \'2\', ..., \'9\'. I want to find all numbers that contain exactly one of each of the numbers in my set.

The problem is: Be

9条回答
  •  半阙折子戏
    2020-12-03 09:43

    import Data.List (inits, tails)
    
    place :: a -> [a] -> [[a]]
    place element list = zipWith (\front back -> front ++ element:back)
                                 (inits list)
                                 (tails list)
    
    perm :: [a] -> [[a]]
    perm = foldr (\element rest -> concat (map (place element) rest)) [[]]
    
    test = perm [1, 3, 14]
    

提交回复
热议问题