Haskell generating all combinations of n numbers

前端 未结 3 1254
深忆病人
深忆病人 2021-02-15 17:38

I am trying to generate all possible combinations of n numbers. For example if n = 3 I would want the following combinations:

(0,0,0), (0,0,1), (0,0,2)... (0,0,9         


        
3条回答
  •  一生所求
    2021-02-15 18:13

    combos 1 list = map (\x -> [x]) list
    combos n list = foldl (++) [] $ map (\x -> map (\y -> x:y) nxt) list
        where nxt = combos (n-1) list
    

    In your case

    combos 3 [0..9]
    

提交回复
热议问题