RosettaCodeData/Task/Permutations/Lambdatalk/permutations-1.lambdatalk

26 lines
862 B
Plaintext

{def inject
{lambda {:x :a}
{if {A.empty? :a}
then {A.new {A.new :x}}
else {let { {:c {{lambda {:a :b} {A.cons {A.first :a} :b}} :a}}
{:d {inject :x {A.rest :a}}}
{:e {A.cons :x :a}}
} {A.cons :e {A.map :c :d}}}}}}
-> inject
{def permut
{lambda {:a}
{if {A.empty? :a}
then {A.new :a}
else {let { {:c {{lambda {:a :b} {inject {A.first :a} :b}} :a}}
{:d {permut {A.rest :a}}}
} {A.reduce A.concat {A.map :c :d}}}}}}
-> permut
{permut {A.new 1 2 3}}
-> [[1,2,3],[2,1,3],[2,3,1],[1,3,2],[3,1,2],[3,2,1]]
{permut {A.new 1 2 3 4}}
->
[[1,2,3,4],[2,1,3,4],[2,3,1,4],[2,3,4,1],[1,3,2,4],[3,1,2,4],[3,2,1,4],[3,2,4,1],[1,3,4,2],[3,1,4,2],[3,4,1,2],[3,4,2,1],[1,2,4,3],[2,1,4,3],[2,4,1,3],[2,4,3,1],[1,4,2,3],[4,1,2,3],[4,2,1,3],[4,2,3,1],[1,4,3,2],[4,1,3,2],[4,3,1,2],[4,3,2,1]]