-----------------------------------------------------------
-- For a list of values, get a list of all permutations of
-- the items in the original list
-- (algorithm produces duplicate permutations for lists
-- containing duplicate elements)
permutations(list) = cond {
case isEmpty(list) []
case isEmpty(tail(list)) [list]
else
for elem in list
for perm in permutations(delete(elem, list))
yield [elem] ++ perm
}
-----------------------------------------------------------
delete(elem, list) = cond {
case isEmpty(list) []
case (head(list) == elem) tail(list)
else [head(list)] ++ delete(elem, tail(list))
}
-----------------------------------------------------------
output =
range(0, 2) -- this example uses the list [0, 1, 2]
|> permutations
|> println
___ ___ ___ ___ ___
/\ \ /\ \ /\ \ /\__\ /\ \
/::\ \ /::\ \ \:\ \ /::| | \:\ \
/:/\:\ \ /:/\:\ \ \:\ \ /:|:| | \:\ \
/::\~\:\ \ /:/ \:\ \ ___ /::\ \ /:/|:| |__ /::\ \
/:/\:\ \:\__\ /:/__/ \:\__\ /\ /:/\:\__\ /:/ |:| /\__\ /:/\:\__\
\/__\:\/:/ / \:\ \ /:/ / \:\/:/ \/__/ \/__|:|/:/ / /:/ \/__/
\::/ / \:\ /:/ / \::/__/ |:/:/ / /:/ /
\/__/ \:\/:/ / \:\ \ |::/ / \/__/
\::/ / \:\__\ /:/ /
\/__/ \/__/ \/__/
___ ___ ___ ___
/\__\ /\ \ /\ \ /\ \ Pointless (0.1.0)
/:/ / /::\ \ /::\ \ /::\ \
/:/ / /:/\:\ \ /:/\ \ \ /:/\ \ \ A scripting language
/:/ / /::\~\:\ \ _\:\~\ \ \ _\:\~\ \ \ for learning and fun
/:/__/ /:/\:\ \:\__\ /\ \:\ \ \__\ /\ \:\ \ \__\
\:\ \ \:\~\:\ \/__/ \:\ \:\ \/__/ \:\ \:\ \/__/ copyright (c) 2020
\:\ \ \:\ \:\__\ \:\ \:\__\ \:\ \:\__\
\:\ \ \:\ \/__/ \:\/:/ / \:\/:/ / Avery N. Nortonsmith
\:\__\ \:\__\ \::/ / \::/ /
\/__/ \/__/ \/__/ \/__/ https://plts.dev
----------
- Enter code in the editor on the left
- Output is displayed here on the right
- (imports and user input disabled are, everything else should work, *albeit slowly*)
----------