Permutations: Difference between revisions

m (→‎{{header|Sidef}}: updated code)
 
(7 intermediate revisions by 3 users not shown)
Line 5,691:
This follows the Go language non-recursive example, but is not limited to integers, or even to numbers.
 
<syntaxhighlight lang="langur">val .factorial = ffn .x: if(.x < 2: 1; .x x* self(.x - 1))
{{works with|langur|0.10}}
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values. 0.10 also parses the increment section of a for loop as a multi-variable assignment, not as a list of assignments.
 
val .permute = ffn(.arrlist) {
<syntaxhighlight lang="langur">val .factorial = f if(.x < 2: 1; .x x self(.x - 1))
if .list is not isArray(.arr)list: throw "expected arraylist"
 
val .permute = f(.arr) {
if not isArray(.arr): throw "expected array"
 
val .limit = 10
if len(.arrlist) > .limit: throw $"permutation limit exceeded (currently \{{.limit;}})"
 
var .elements = .arrlist
var .ordinals = pseries len .elements
 
Line 5,708 ⟶ 5,705:
var .i, .j
 
for[.p=[.arrlist]] of .factorial(len .arrlist)-1 {
.i = .n - 1
.j = .n
Line 5,732 ⟶ 5,729:
for .e in .permute([1, 3.14, 7]) {
writeln .e
}
}</syntaxhighlight>
 
{{out}}
Line 9,411 ⟶ 9,409:
st> 'Abc' permutations contents
('bcA' 'cbA' 'cAb' 'Acb' 'bAc' 'Abc' )
</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">
fun interleave x [] = [[x]]
| interleave x (y::ys) = (x::y::ys) :: (List.map (fn a => y::a) (interleave x ys))
 
fun perms [] = [[]]
| perms (x::xs) = List.concat (List.map (interleave x) (perms xs))
</syntaxhighlight>
 
Line 9,886 ⟶ 9,893:
===Recursive===
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var permute // recursive
permute = Fn.new { |input|
if (input.count == 1) return [input]
Line 9,922 ⟶ 9,929:
{{libheader|Wren-math}}
Output modified to follow the pattern of the recursive version.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var input = [1, 2, 3]
Line 9,964 ⟶ 9,971:
===Library based===
{{libheader|Wren-perm}}
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Perm
 
var a = [1, 2, 3]
890

edits