1
$\begingroup$

This question relates to the recent question in: Creating permutations on a sublist of the list {1, 2, ..., n}

The following was suggested to create rules, then permutations using these rules:

list = Range[7] permutations = Thread[{1, 3, 7} -> #] & /@ Permutations[{1, 3, 7}] list /. permutations 

This results in:

{{1, 2, 3, 4, 5, 6, 7}, {1, 2, 7, 4, 5, 6, 3}, {3, 2, 1, 4, 5, 6, 7}, {3, 2, 7, 4, 5, 6, 1}, {7, 2, 1, 4, 5, 6, 3}, {7, 2, 3, 4, 5, 6, 1}} 

In other words, the list {1,2,3,4,5,6,7} is permuted in the positions 1, 3 and 7 in all possible ways.

I'd like to execute this operation repeatedly on the list {1,2,3,4,5,6,7} and collect all outcomes in a single list. For instance:

Given the list of lists {{1,3,7}, {3,4,6,7}} (which can have more than 2 elements) I need the output:

{{1, 2, 3, 4, 5, 6, 7}, {1, 2, 7, 4, 5, 6, 3}, {3, 2, 1, 4, 5, 6, 7}, {3, 2, 7, 4, 5, 6, 1}, {7, 2, 1, 4, 5, 6, 3}, {7, 2, 3, 4, 5, 6, 1}, {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 7, 6}, {1, 2, 3, 6, 5, 4, 7}, {1, 2, 3, 6, 5, 7, 4}, {1, 2, 3, 7, 5, 4, 6}, {1, 2, 3, 7, 5, 6, 4}, {1, 2, 4, 3, 5, 6, 7}, {1, 2, 4, 3, 5, 7, 6}, {1, 2, 4, 6, 5, 3, 7}, {1, 2, 4, 6, 5, 7, 3}, {1, 2, 4, 7, 5, 3, 6}, {1, 2, 4, 7, 5, 6, 3}, {1, 2, 6, 3, 5, 4, 7}, {1, 2, 6, 3, 5, 7, 4}, {1, 2, 6, 4, 5, 3, 7}, {1, 2, 6, 4, 5, 7, 3}, {1, 2, 6, 7, 5, 3, 4}, {1, 2, 6, 7, 5, 4, 3}, {1, 2, 7, 3, 5, 4, 6}, {1, 2, 7, 3, 5, 6, 4}, {1, 2, 7, 4, 5, 3, 6}, {1, 2, 7, 4, 5, 6, 3}, {1, 2, 7, 6, 5, 3, 4}, {1, 2, 7, 6, 5, 4, 3}} 

In this output, the first six sublists are the original output for {1,3,7}, now combined with the 24 outputs for {3,4,6,7}.

I tried to give the pure function two arguments:

list = Range[7] templist = {{1,3,7}, {3,4,6,7}} permutations = (Thread[#1 -> #2] & /@ Permutations[#1])& /@ templist list /. permutations 

This fails. I cannot get the arguments to be passed correctly to #1 and #2. Can you clarify how multiple arguments can be used properly for the case of pure functions in this example?

$\endgroup$
3
  • $\begingroup$ Do you really want the duplicates in your desired result? $\endgroup$ Commented Aug 25 at 19:13
  • $\begingroup$ No, I added DeleteDuplicates to the code. $\endgroup$ Commented Aug 26 at 10:05
  • $\begingroup$ Related: Pure function inside another pure function $\endgroup$ Commented Aug 26 at 14:20

1 Answer 1

2
$\begingroup$

This is because you try to refer to the list of permutations with #2 but to templist with #1. Pure functions need to take their arguments from the same "scope".

Circumvent this by defining named arguments, e.g.:

list = Range[7] templist = {{1, 3, 7}, {3, 4, 6, 7}} permutations = Function[pList, (Thread[pList -> #] & /@ Permutations[pList])] /@ templist Catenate[list /. permutations] ``` 
$\endgroup$
3
  • $\begingroup$ Thanks! When you write: Pure functions need to take their arguments from the same "scope": this seems to be indicating the arguments need to come from the same collection (or have the same type?). In your code, you use Function[pList, .... It introduces a new variable which will take values from templist and then operate on these before applying (Thread[pList ->#] to Permutations[pList]. This seems the important aspect, setting pList first? Rather than ensuring the same scope? You use two scopes so to speak, setting pList from one scope and # from the next? $\endgroup$ Commented Aug 25 at 18:52
  • 1
    $\begingroup$ Pretty much, yes. Function[plist, ...] iterates over templist here, whereas the pure function Thread[pList->#]& iterates over the list of permutations. The original syntax using Thread[#1->#2]& would still iterate over the list of permutations, but would expect a sequence of arguments for each element of the list rather than a list. In any case, this pure function is not aware of anything beyond the Permutations[...], so you cannot refer to the second loop with this. $\endgroup$ Commented Aug 26 at 14:33
  • 1
    $\begingroup$ Looking at the related question mentioned by @WReach, it seems like there are some possibilities to craft this with what I call "pure functions" (i.e., functions using slots as arguments, though it seems like all anonymous functions are pure for Mathematica). So technically you might be able to use some combination of slots. I'd still go with named arguments, though. $\endgroup$ Commented Aug 26 at 14:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.