# “&” instead of Function[ … ]

WRI provides the code below as a "neat example" of Fold. The code produces all of the subsets of {a, b, c}. As an exercise, I've been trying to replace the Function[ ..] with the ampersand form, but no luck yet. Thanks.

The original example:

Fold[Function[{s, e}, Join[s, Append[#, e] & /@ s]], {{}}, {a, b, c}]

• In this case, you have to make a choice on which should be in "ampersand form", since the Append[] is dependent on both the inner and outer Function[] constructs. – J. M. is away Sep 8 '17 at 16:24
• Fold[Join[#, Function[x, Append[x, #2]] /@ #] &, {{}}, {a, b, c}]? – kglr Sep 8 '17 at 16:34
• ...and kglr's comment gives the other "choice" I was talking about. – J. M. is away Sep 8 '17 at 16:35
• ... or Fold[Join[#, (x \[Function] Append[x, #2]) /@ #] &, {{}}, {a, b, c}] – kglr Sep 8 '17 at 16:36
• Rabbit, I notice that you removed the Accept from my answer. May I know why? – Mr.Wizard Mar 2 '18 at 16:34

In this case you can use the operator form of Append, assuming version 10 or later:

Fold[Join[#, Append[#2] /@ #] &, {{}}, {a, b, c}]

{{}, {a}, {b}, {a, b}, {c}, {a, c}, {b, c}, {a, b, c}}

Fold[Join[#, Replace[#, x_List :> Join[x, {#2}], 1]] &, {{}}, {a, b, c}]


{{}, {a}, {b}, {a, b}, {c}, {a, c}, {b, c}, {a, b, c}}

Or

Fold[Join[#, Function[x, Append[x, #2]] /@ #] &, {{}}, {a, b, c}]


{{}, {a}, {b}, {a, b}, {c}, {a, c}, {b, c}, {a, b, c}}

Or

Fold[Join[#, (x \[Function] Append[x, #2]) /@ #] &, {{}}, {a, b, c}]


{{}, {a}, {b}, {a, b}, {c}, {a, c}, {b, c}, {a, b, c}}