Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Scoped let expression with pattern variables
ResourceFunction["LetScope"][set,expr] binds pattern variables in set to substitute in expr. | |
ResourceFunction["LetScope"][set1,set2,…,expr] uses multiple nested set patterns. |
Pattern match and use the pattern variable:
In[1]:= | ![]() |
Out[1]= | ![]() |
Use any pattern:
In[2]:= | ![]() |
Out[2]= | ![]() |
Nest multiple patterns:
In[3]:= | ![]() |
Out[3]= | ![]() |
Use LeftArrow (←) as an alternative symbol for let bindings:
In[4]:= | ![]() |
Out[4]= | ![]() |
Unmatched variables bind to Sequence[]:
In[5]:= | ![]() |
Out[5]= | ![]() |
Pattern variables inside Verbatim are ignored:
In[6]:= | ![]() |
Out[6]= | ![]() |
Unevaluated can be used to delay evaluation:
In[7]:= | ![]() |
Out[7]= | ![]() |
Variable names can be reused:
In[8]:= | ![]() |
Out[8]= | ![]() |
Use intermediate non-binding expressions to evaluate in between:
In[9]:= | ![]() |
Out[9]= | ![]() |
Because LetScope is HoldAllComplete, Sequence can be returned:
In[10]:= | ![]() |
Out[10]= | ![]() |
Use LetScope as a scope with support for binding to multiple variables:
In[11]:= | ![]() |
Out[11]= | ![]() |
Use LetScope for destructuring any expression with pattern matching:
In[12]:= | ![]() |
Out[12]= | ![]() |
In[13]:= | ![]() |
Out[13]= | ![]() |
Compound nested scope with dependencies:
In[14]:= | ![]() |
Out[14]= | ![]() |
For a single binding LetScope is equivalent to a substitution with Replace:
In[15]:= | ![]() |
Out[15]= | ![]() |
In[16]:= | ![]() |
Out[16]= | ![]() |
For a single variable left-hand-side LetScope is similar to ResourceFunction["CompoundScope"]:
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
LetScope effectively applies binding rules (similar to ResourceFunction["PatternBindings"]) to consequent expressions:
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
In[21]:= | ![]() |
Out[21]= | ![]() |
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License