1
$\begingroup$

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?

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

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}}]}]]
$\endgroup$
  • $\begingroup$ Clever. I figured the answer would involve multiple statements if there was no elegant way that I just didn't know of. Thank you. $\endgroup$ – ericmarkmartin Jun 26 '16 at 2:32
0
$\begingroup$

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 @@.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.