Products
  • Wolfram|One

    The definitive Wolfram Language and notebook experience

  • Mathematica

    The original technical computing environment

  • Notebook Assistant + LLM Kit

    All-in-one AI assistance for your Wolfram experience

  • Compute Services
  • System Modeler
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App

More mobile apps

Core Technologies of Wolfram Products

  • Wolfram Language
  • Computable Data
  • Wolfram Notebooks
  • AI & Linguistic Understanding

Deployment Options

  • Wolfram Cloud
  • wolframscript
  • Wolfram Engine Community Edition
  • Wolfram LLM API
  • WSTPServer
  • Wolfram|Alpha APIs

From the Community

  • Function Repository
  • Community Paclet Repository
  • Example Repository
  • Neural Net Repository
  • Prompt Repository
  • Wolfram Demonstrations
  • Data Repository
  • Group & Organizational Licensing
  • All Products
Consulting & Solutions

We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

  • Data & Computational Intelligence
  • Model-Based Design
  • Algorithm Development
  • Wolfram|Alpha for Business
  • Blockchain Technology
  • Education Technology
  • Quantum Computation

Wolfram Consulting

Wolfram Solutions

  • Data Science
  • Artificial Intelligence
  • Biosciences
  • Healthcare Intelligence
  • Sustainable Energy
  • Control Systems
  • Enterprise Wolfram|Alpha
  • Blockchain Labs

More Wolfram Solutions

Wolfram Solutions For Education

  • Research Universities
  • Colleges & Teaching Universities
  • Junior & Community Colleges
  • High Schools
  • Educational Technology
  • Computer-Based Math

More Solutions for Education

  • Contact Us
Learning & Support

Get Started

  • Wolfram Language Introduction
  • Fast Intro for Programmers
  • Fast Intro for Math Students
  • Wolfram Language Documentation

More Learning

  • Highlighted Core Areas
  • Demonstrations
  • YouTube
  • Daily Study Groups
  • Wolfram Schools and Programs
  • Books

Grow Your Skills

  • Wolfram U

    Courses in computing, science, life and more

  • Community

    Learn, solve problems and share ideas.

  • Blog

    News, views and insights from Wolfram

  • Resources for

    Software Developers

Tech Support

  • Contact Us
  • Support FAQs
  • Support FAQs
  • Contact Us
Company
  • About Wolfram
  • Career Center
  • All Sites & Resources
  • Connect & Follow
  • Contact Us

Work with Us

  • Student Ambassador Initiative
  • Wolfram for Startups
  • Student Opportunities
  • Jobs Using Wolfram Language

Educational Programs for Adults

  • Summer School
  • Winter School

Educational Programs for Youth

  • Middle School Camp
  • High School Research Program
  • Computational Adventures

Read

  • Stephen Wolfram's Writings
  • Wolfram Blog
  • Wolfram Tech | Books
  • Wolfram Media
  • Complex Systems

Educational Resources

  • Wolfram MathWorld
  • Wolfram in STEM/STEAM
  • Wolfram Challenges
  • Wolfram Problem Generator

Wolfram Initiatives

  • Wolfram Science
  • Wolfram Foundation
  • History of Mathematics Project

Events

  • Stephen Wolfram Livestreams
  • Online & In-Person Events
  • Contact Us
  • Connect & Follow
Wolfram|Alpha
  • Your Account
  • User Portal
  • Wolfram Cloud
  • Products
    • Wolfram|One
    • Mathematica
    • Notebook Assistant + LLM Kit
    • Compute Services
    • System Modeler
    • Finance Platform
    • Wolfram|Alpha Notebook Edition
    • Application Server
    • Enterprise Private Cloud
    • Wolfram Engine
    • Wolfram Player
    • Wolfram Cloud App
    • Wolfram Player App

    More mobile apps

    • Core Technologies
      • Wolfram Language
      • Computable Data
      • Wolfram Notebooks
      • AI & Linguistic Understanding
    • Deployment Options
      • Wolfram Cloud
      • wolframscript
      • Wolfram Engine Community Edition
      • Wolfram LLM API
      • WSTPServer
      • Wolfram|Alpha APIs
    • From the Community
      • Function Repository
      • Community Paclet Repository
      • Example Repository
      • Neural Net Repository
      • Prompt Repository
      • Wolfram Demonstrations
      • Data Repository
    • Group & Organizational Licensing
    • All Products
  • Consulting & Solutions

    We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

    WolframConsulting.com

    Wolfram Solutions

    • Data Science
    • Artificial Intelligence
    • Biosciences
    • Healthcare Intelligence
    • Sustainable Energy
    • Control Systems
    • Enterprise Wolfram|Alpha
    • Blockchain Labs

    More Wolfram Solutions

    Wolfram Solutions For Education

    • Research Universities
    • Colleges & Teaching Universities
    • Junior & Community Colleges
    • High Schools
    • Educational Technology
    • Computer-Based Math

    More Solutions for Education

    • Contact Us
  • Learning & Support

    Get Started

    • Wolfram Language Introduction
    • Fast Intro for Programmers
    • Fast Intro for Math Students
    • Wolfram Language Documentation

    Grow Your Skills

    • Wolfram U

      Courses in computing, science, life and more

    • Community

      Learn, solve problems and share ideas.

    • Blog

      News, views and insights from Wolfram

    • Resources for

      Software Developers
    • Tech Support
      • Contact Us
      • Support FAQs
    • More Learning
      • Highlighted Core Areas
      • Demonstrations
      • YouTube
      • Daily Study Groups
      • Wolfram Schools and Programs
      • Books
    • Support FAQs
    • Contact Us
  • Company
    • About Wolfram
    • Career Center
    • All Sites & Resources
    • Connect & Follow
    • Contact Us

    Work with Us

    • Student Ambassador Initiative
    • Wolfram for Startups
    • Student Opportunities
    • Jobs Using Wolfram Language

    Educational Programs for Adults

    • Summer School
    • Winter School

    Educational Programs for Youth

    • Middle School Camp
    • High School Research Program
    • Computational Adventures

    Read

    • Stephen Wolfram's Writings
    • Wolfram Blog
    • Wolfram Tech | Books
    • Wolfram Media
    • Complex Systems
    • Educational Resources
      • Wolfram MathWorld
      • Wolfram in STEM/STEAM
      • Wolfram Challenges
      • Wolfram Problem Generator
    • Wolfram Initiatives
      • Wolfram Science
      • Wolfram Foundation
      • History of Mathematics Project
    • Events
      • Stephen Wolfram Livestreams
      • Online & In-Person Events
    • Contact Us
    • Connect & Follow
  • Wolfram|Alpha
  • Wolfram Cloud
  • Your Account
  • User Portal
Wolfram Language & System Documentation Center
Patterns and Transformation Rules
TECH NOTE

Patterns and Transformation Rules

PatternsClearing and Removing Objects
AssignmentsTransformation Rules
Types of Values
Patterns
Patterns stand for classes of expressions. They contain pattern objects that represent sets of possible expressions.
_
any expression
x_
any expression, given the name x
x:pattern
a pattern, given the name x
pattern?test
a pattern that yields True when test is applied to its value
_h
any expression with head h
x_h
any expression with head h, given the name x
__
any sequence of one or more expressions
___
any sequence of zero or more expressions
x__
and
x___
sequences of expressions, given the name x
__h
and
___h
sequences of expressions, each with head h
x__h
and
x___h
sequences of expressions with head h, given the name x
PatternSequence[p1,p2,…]
a sequence of patterns
OrderlessPatternSequence[p1,p2,…]
an orderless sequence of patterns
KeyValuePattern[{key1val1,…}]
an orderless sequence of keyivali pairs
x_:v
an expression with default value v
x_h:v
an expression with head h and default value v
x_.
an expression with a globally defined default value
Optional[x_h]
an expression that must have head h, and has a globally defined default value
Except[c]
any expression except one that matches c
Except[c,pattern]
any expression matching pattern, except one that matches c
pattern..
a pattern repeated one or more times
pattern...
a pattern repeated zero or more times
Repeated[pattern, spec]
a pattern repeated according to spec
pattern1|pattern2|…
a pattern that matches at least one of the patterni
pattern/;cond
a pattern for which cond evaluates to True
HoldPattern[pattern]
a pattern not evaluated
Verbatim[expr]
an expression to be matched verbatim
OptionsPattern[]
a sequence of options
Longest[pattern]
the longest sequence consistent with pattern
Shortest[pattern]
the shortest sequence consistent with pattern
Pattern objects.
When several pattern objects with the same name occur in a single pattern, all the objects must stand for the same expression. Thus f[x_,x_] can stand for f[2,2] but not f[2,3].
In a pattern object such as _h, the head h can be any expression, but cannot itself be a pattern.
A pattern object such as x__ stands for a sequence of expressions. So, for example, f[x__] can stand for f[a,b,c], with x being Sequence[a,b,c]. If you use x, say in the result of a transformation rule, the sequence will be spliced into the function in which x appears. Thus g[u,x,u] would become g[u,a,b,c,u].
When the pattern objects x_:v and x_. appear as arguments of functions, they represent arguments which may be omitted. When the argument corresponding to x_:v is omitted, x is taken to have value v. When the argument corresponding to x_. is omitted, x is taken to have a default value that is associated with the function in which it appears. You can specify this default value by making assignments for Default[f] and so on.
Default[f]
default value for x_. when it appears as any argument of the function f
Default[f,n]
default value for x_. when it appears as the n th argument (negative n count from the end)
Default[f,n,tot]
default value for the n th argument when there are a total of tot arguments
Default values.
A pattern like f[x__,y__,z__] can match an expression like f[a,b,c,d,e] with several different choices of x, y, and z. The choices with x and y of minimum length are tried first. In general, when there are multiple __ or ___ in a single function, the case that is tried first takes all the __ and ___ to stand for sequences of minimum length, except the last one, which stands for "the rest" of the arguments.
When x_:v or x_. are present, the case that is tried first is the one in which none of them correspond to omitted arguments. Cases in which later arguments are dropped are tried next.
The order in which the different cases are tried can be changed using Shortest and Longest.
Orderless
f[x,y] and f[y,x] are equivalent
Flat
f[f[x],y] and f[x,y] are equivalent
OneIdentity
f[x] and x are equivalent
Attributes used in matching patterns.
Pattern objects like x_ can represent any sequence of arguments in a function f with attribute Flat. The value of x in this case is f applied to the sequence of arguments. If f has the attribute OneIdentity, then e is used instead of f[e] when x corresponds to a sequence of just one argument.
Assignments
lhs=rhs
immediate assignment: rhs is evaluated at the time of assignment
lhs:=rhs
delayed assignment: rhs is evaluated when the value of lhs is requested
The two basic types of assignment in the Wolfram Language.
Assignments in the Wolfram Language specify transformation rules for expressions. Every assignment that you make must be associated with a particular Wolfram Language symbol.
f[args]=rhs
assignment is associated with f (downvalue)
t/:f[args]=rhs
assignment is associated with t (upvalue)
f[g[args]]^=rhs
assignment is associated with g (upvalue)
Assignments associated with different symbols.
In the case of an assignment like f[args]=rhs, the Wolfram Language looks at f, then the head of f, then the head of that, and so on, until it finds a symbol with which to associate the assignment.
When you make an assignment like lhs^=rhs, the Wolfram Language will set up transformation rules associated with each distinct symbol that occurs either as an argument of lhs, or as the head of an argument of lhs.
The transformation rules associated with a particular symbol s are always stored in a definite order, and are tested in that order when they are used. Each time you make an assignment, the corresponding transformation rule is inserted at the end of the list of transformation rules associated with s, except in the following cases:
  • The left‐hand side of the transformation rule is identical to a transformation rule that has already been stored, and any /; conditions on the right‐hand side are also identical. In this case, the new transformation rule is inserted in place of the old one.
  • The Wolfram Language determines that the new transformation rule is more specific than a rule already present, and would never be used if it were placed after this rule. In this case, the new rule is placed before the old one. Note that in many cases it is not possible to determine whether one rule is more specific than another; in such cases, the new rule is always inserted at the end.
Types of Values
Attributes[f]
attributes of f
DefaultValues[f]
default values for arguments of f
DownValues[f]
values for f[…], f[…][…], etc.
FormatValues[f]
print forms associated with f
Messages[f]
messages associated with f
NValues[f]
numerical values associated with f
Options[f]
defaults for options associated with f
OwnValues[f]
values for f itself
UpValues[f]
values for …[…,f[…],…]
Types of values associated with symbols.
Clearing and Removing Objects
expr=.
clear a value defined for expr
f/:expr=.
clear a value associated with f defined for expr
Clear[s1,s2,…]
clear all values for the symbols si, except for attributes, messages, and defaults
ClearAll[s1,s2,…]
clear all values for the si, including attributes, messages, and defaults
Remove[s1,s2,…]
clear all values, and then remove the names of the si
Ways to clear and remove objects.
In Clear, ClearAll, and Remove, each argument can be either a symbol or the name of a symbol as a string. String arguments can contain the metacharacters * and @ to specify action on all symbols whose names match the pattern.
Clear, ClearAll, and Remove do nothing to symbols with the attribute Protected.
Transformation Rules
lhs->rhs
immediate rule: rhs is evaluated when the rule is first given
lhs:>rhs
delayed rule: rhs is evaluated when the rule is used
The two basic types of transformation rules in the Wolfram Language.
Replacements for pattern variables that appear in transformation rules are effectively done using ReplaceAll (the /. operator).

Related Guides

    ▪
  • Patterns
  • ▪
  • Rules & Patterns

Related Tech Notes

    ▪
  • Introduction to Patterns
  • ▪
  • Pattern Sequences
  • ▪
  • Verbatim Patterns
  • ▪
  • Optional and Default Arguments

Related Wolfram Training Courses

  • ▪ Mathematica: An Introduction
  • ▪ Mathematica: Programming in the Wolfram Language
Top
Introduction for Programmers
Introductory Book
Wolfram Function Repository | Wolfram Data Repository | Wolfram Data Drop | Wolfram Language Products
Top
  • Products
  • Wolfram|One
  • Mathematica
  • Notebook Assistant + LLM Kit
  • Compute Services
  • System Modeler

  • Wolfram|Alpha Notebook Edition
  • Wolfram|Alpha Pro
  • Mobile Apps

  • Wolfram Engine
  • Wolfram Player

  • Volume & Site Licensing
  • Server Deployment Options
  • Consulting
  • Wolfram Consulting
  • Repositories
  • Data Repository
  • Function Repository
  • Community Paclet Repository
  • Neural Net Repository
  • Prompt Repository

  • Wolfram Language Example Repository
  • Notebook Archive
  • Wolfram GitHub
  • Learning
  • Wolfram U
  • Wolfram Language Documentation
  • Webinars & Training
  • Educational Programs

  • Wolfram Language Introduction
  • Fast Introduction for Programmers
  • Fast Introduction for Math Students
  • Books

  • Wolfram Community
  • Wolfram Blog
  • Public Resources
  • Wolfram|Alpha
  • Wolfram Problem Generator
  • Wolfram Challenges

  • Computer-Based Math
  • Computational Thinking
  • Computational Adventures

  • Demonstrations Project
  • Wolfram Data Drop
  • MathWorld
  • Wolfram Science
  • Wolfram Media Publishing
  • Customer Resources
  • Store
  • Product Downloads
  • User Portal
  • Your Account
  • Organization Access

  • Support FAQ
  • Contact Support
  • Company
  • About Wolfram
  • Careers
  • Contact
  • Events
Wolfram Community Wolfram Blog
Legal & Privacy Policy
WolframAlpha.com | WolframCloud.com
© 2026 Wolfram
© 2026 Wolfram | Legal & Privacy Policy |
English