# All permutations example in Erlang

**URL:** <https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380>\
**Category:** New to Julia\
**Tags:** erlang\
**Created:** [November 12, 2021, 1:55pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380 "2021-11-12T13:55:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [November 12, 2021, 1:55pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380/1 "2021-11-12T13:55:05Z")

</div>

Wondering how this compact [Erlang code](https://stackoverflow.com/questions/34179283/permutations-example-in-erlang/)

```julia
 perms([]) -> [[]];
 perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].

```

could look in Julia language.

[Common Lisp](https://rosettacode.org/wiki/Permutations#Common_Lisp) apparently uses the same logic, but is more verbose:

```julia
(defun permute (list)
  (if list
    (mapcan #'(lambda (x)
		(mapcar #'(lambda (y) (cons x y))
			(permute (remove x list))))
	    list)
    '(()))) ; else
 
(print (permute '(A B Z)))

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 12, 2021, 2:22pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380/2 "2021-11-12T14:22:23Z")

</div>

```julia
perms(l) = isempty(l) ? [l] : [[x; y] for x in l for y in perms(setdiff(l, x))]

```

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [November 12, 2021, 2:32pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380/3 "2021-11-12T14:32:30Z")

</div>

Thanks Bogumił!

I was close… :

- wrote `isempty(l) ? []`.
- put a comma in [x,y], gives no error, but quite different output

Maybe good for a training based on debugging.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 12, 2021, 2:35pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380/4 "2021-11-12T14:35:12Z")

</div>

The issue is that you want to return the _collection_ of permutations. Just returning `[]` does not return any permutation, while you want to return a single empty permutation `[[]]`. The reason I use `[l]` and not `[[]]` is that `[l]` does a proper inference of type of the collection to be produced (and we know that `l` is empty so `[l]` will contain one empty collection but with the benefit that it has a proper element type).

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [November 12, 2021, 2:37pm UTC](https://discourse.julialang.org/t/all-permutations-example-in-erlang/71380/5 "2021-11-12T14:37:20Z")

</div>

Allright!
