How can I get permutations of a list?

前端 未结 5 2024
梦如初夏
梦如初夏 2020-12-10 04:55

How can I get permutations of a list in Elixir?

Eg, for [\"a\", \"b\", \"c\"], I would expect:

# [[\"a\", \"b\", \"c\"], [\"a\", \"c\",          


        
5条回答
  •  旧时难觅i
    2020-12-10 05:41

    Like this:

    defmodule Permutations do
      def of([]) do
        [[]]
      end
    
      def of(list) do
        for h <- list, t <- of(list -- [h]), do: [h | t]
      end
    end
    

提交回复
热议问题