Skip to main content

programming - What's the purpose of the Unique function?


As the title says: What's the purpose of Unique?




  • I understand that it generates some unique symbol, but when and for what is it to be used? Are there applications in practical solutions, or is it for the backend of programs (I noticed that Block etc use variable names similar to what Unique creates)?

  • How do I use a variable generated by Unique? Do I store its name in another variable and then use that as a pointer of some sort by converting its content (e.g. $123) to an expression somehow?



Answer



Probably the most common use of Unique is in situations when you need a large number of local variables (and sometimes a variable number of local variables) so using Module is either inconvenient or impossible. In that case you can use the construction: vars= Table[Unique[x],{n}] or something of this kind. You can find a few examples in the archives of the MathGroup. One that I remember being quite pleased with myself can be found here:


http://mathforum.org/kb/thread.jspa?forumID=79&threadID=1185003&messageID=3868818


Comments