Rebol3 Code Examplex
Permutations
Write a program that generates all permutations of n different objects. (Practically numerals!)
Rebol [
title: "Rosetta code: Permutations"
file: %Permutations.r3
url: https://rosettacode.org/wiki/Permutations
]
permutations: function [
{Return block of all permutations of a block}
items [block!]
][
if single? items [return reduce [copy items]]
result: copy []
repeat i length? items [
rest: copy items
elem: take at rest i ;; pick each element as head
foreach perm permutations rest [
repend result [append reduce [elem] perm]
]
]
result
]
probe permutations [1 2 3]
probe new-line/all permutations ["aardvarks" "eat" "ants"] onOutput:
[[1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 1 2] [3 2 1]]
[
["aardvarks" "eat" "ants"]
["aardvarks" "ants" "eat"]
["eat" "aardvarks" "ants"]
["eat" "ants" "aardvarks"]
["ants" "aardvarks" "eat"]
["ants" "eat" "aardvarks"]
]