# Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the following code.

{a, b, c, d, e} = CirclePoints[5]
Graphics[{
{PointSize@Medium, Point[{a, b, c, d, e}]},
Line[{
{a, b}, {a, c}, {a, d}, {a, e},
{b, c}, {b, d},
{c, e}}]
}]


Which results in this graphic graph http://gdurl.com/noVC

However, this becomes frustrating as a, b, c, d, and e are not bound to the Graphics call. Ideally, I'd like to be able to do something like:

With[{
{a, b, c, d, e} = CirclePoints[5]
}, Graphics[{
{PointSize@Medium, Point[{a, b, c, d, e}]},
Line[{
{a, b}, {a, c}, {a, d}, {a, e},
{b, c}, {b, d},
{c, e}}]
}]
]


However, this results in an error

"Local variable specification {{a,b,c,d,e}=CirclePoints[5]} contains {a,b,c,d,e}=CirclePoints[5], which is an assignment to {a,b,c,d,e}; only assignments to symbols are allowed."

which makes sense, given that With does not have the desired capability, and neither do Block and Module.

So, is there a way to do this?

• Won't Thread directly solve your problem? – Wjx Jun 26 '16 at 4:17
• How so? I don't know if threaded assignments work in With. – ericmarkmartin Jun 26 '16 at 4:19

The answer is no. The 1st argument of With does not allow destructuring. It only accepts a list of simple assignments.

I would rewrite the example you give as

Module[{a, b, c, d, e},
{a, b, c, d, e} = CirclePoints[5];
Graphics[
{{PointSize @ Medium, Point[{a, b, c, d, e}]},
Line[{{a, b}, {a, c}, {a, d}, {a, e}, {b, c}, {b, d}, {c, e}}]}]]

• Clever. I figured the answer would involve multiple statements if there was no elegant way that I just didn't know of. Thank you. – ericmarkmartin Jun 26 '16 at 2:32

You could do this:

Function[{a, b, c, d, e},
Graphics[{{PointSize@Medium, Point[{a, b, c, d, e}]},
Line[{{a,b}, {a,c}, {a,d}, {a,e}, {b,c}, {b,d}, {c, e}}]}]]@@CirclePoints[5]


I used a Function to localize the variables and then applied it to the desired points with @@.