How to resolve the algorithm Permutations step by step in the Erlang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Permutations step by step in the Erlang programming language
Table of Contents
Problem Statement
Write a program that generates all permutations of n different objects. (Practically numerals!)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Permutations step by step in the Erlang programming language
Source code in the erlang programming language
-module(permute).
-export([permute/1]).
permute([]) -> [[]];
permute(L) -> [[X|Y] || X<-L, Y<-permute(L--[X])].
F = fun(L) -> G = fun(_, []) -> [[]]; (F, L) -> [[X|Y] || X<-L, Y<-F(F, L--[X])] end, G(G, L) end.
-module(permute).
-export([permute/1]).
permute([]) -> [[]];
permute(L) -> zipper(L, [], []).
% Use zipper to pick up first element of permutation
zipper([], _, Acc) -> lists:reverse(Acc);
zipper([H|T], R, Acc) ->
% place current member in front of all permutations
% of rest of set - both sides of zipper
prepend(H, permute(lists:reverse(R, T)),
% pass zipper state for continuation
T, [H|R], Acc).
prepend(_, [], T, R, Acc) -> zipper(T, R, Acc); % continue in zipper
prepend(X, [H|T], ZT, ZR, Acc) -> prepend(X, T, ZT, ZR, [[X|H]|Acc]).
main(_) -> io:fwrite("~p~n", [permute:permute([1,2,3])]).
You may also check:How to resolve the algorithm Inverted index step by step in the Java programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Aikido programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Julia programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Ring programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Arturo programming language