Skip to main content
26 votes
Accepted

How to replace all elements of a list by a rule "element" -> "element_"

Here is one way: Pattern[#, Blank[]] & /@ {a, b, c, d, e, f, g, h, i, j} (* {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} *) An inspection of the ...
WReach's user avatar
  • 69.1k
17 votes

How to replace all elements of a list by a rule "element" -> "element_"

A few additional alternatives to inject patterns on the rhs: list = {a, b, c, d, e, f, g, h, i, j}; Replace[list, a_ :> (x_ /. x -> a), 1] {a_, b_, c_, ...
kglr's user avatar
  • 398k
15 votes
Accepted

How to check the validity of an option value

Implementation This is indeed an important problem. It is usually best to have a separate function testing various options. Here is the solution I propose: a wrapper that would factor out the testing ...
Leonid Shifrin's user avatar
15 votes
Accepted

How do I correctly apply patterns to function arguments using Pattern (:), PatternTest (?), and Condition (/;)

This is somewhat subtle, but really it's just a syntax issue. If you want to use the colon for pattern-matching, leave off the underscore, i.e., ...
march's user avatar
  • 23.9k
14 votes

Why is this syntax for patterns with Optional values not supported?

The Documentation states (emphasis is mine): Pattern (:): s:obj represents the pattern object ...
Alexey Popkov's user avatar
13 votes
Accepted

A hack for aggressive type-checking?

With the definition f[x_List] := x + 1 you create a substitution rule that can only be applied when the argument of f is a <...
Fred Simons's user avatar
  • 10.2k
13 votes
Accepted

How to check the number of function arguments given by the user

You can redefine your function f to accept a variable number of arguments (x__) and then check the length with ...
Domen's user avatar
  • 27.3k
13 votes
Accepted

Map a two argument function to every element on list

Since you're looking to learn more about functional style, I'll elaborate rather than just provide an answer. We start with our table: ...
lericr's user avatar
  • 30.2k
12 votes
Accepted

Confused by the opts : OptionsPattern[ ] pattern

Re 1: As documented in Operator Input Forms, colon represents two different operations: symb:expr Pattern[symb, expr] patt:expr Optional[patt, expr] If you ...
Itai Seggev's user avatar
  • 14.2k
12 votes

How to avoid repeated pattern tests in function definitions

You can use PatternSequence: f[PatternSequence[x_,y_,z_]?NumericQ, PatternSequence[k_,l_]?IntegerQ] := stuff[x,y,z,k,l] Check:...
Carl Woll's user avatar
  • 131k
12 votes
Accepted

Optional argument that can be completely omitted?

See @AlbertRetey's answer for all but trivial cases. Don't use Optional, nor If. Use two definitions. ...
Szabolcs's user avatar
  • 236k
12 votes
Accepted

How to restrict the domain of a function

I think the easiest way is to use Condition which has the operator form /;. ...
m_goldberg's user avatar
  • 108k
11 votes

Optional argument that can be completely omitted?

for your simple example Szabolcs suggestions is certainly the best you can do. If for some reason in a less simple situation you want the behavior you described with just one definition this is what ...
Albert Retey's user avatar
  • 23.6k
11 votes

A hack for aggressive type-checking?

In general there are multiple reasons why a function could fail and it would be better to know the specific reason--particularly if it has been some time since the code was written. See the ...
Bob Hanlon's user avatar
  • 160k
11 votes
Accepted

How to set deeper patterns in arguments within function definitions. For example if an argument must be a list of 1 or more strings

When imposing restrictions on patterns, there are typically two options: Use Condition (/;) to specify an arbitrary expression ...
Lukas Lang's user avatar
  • 34.2k
10 votes
Accepted

Multiple pattern test in the definition of a function

I was going for a syntactic pattern thing, but actually the two-argument form of MatrixQ should work here: ...
LLlAMnYP's user avatar
  • 11.5k
10 votes
Accepted

What does _. mean in patterns?

FullForm will show you how an expression is really interpreted, In[5]:= FullForm[_.] Out[5]= Optional[Blank[]] This tells ...
Jason B.'s user avatar
  • 68.9k
10 votes

Appending _?NumericQ automatically to every variable of a function in the definition

If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use ...
Carl Woll's user avatar
  • 131k
10 votes
Accepted

A problem on the pattern (:) of Heads

Exp[7] gets evaluated to Power[E, 7], so it doesn't match the pattern you've defined for f. Exp[7.7] gets evaluated to a finite precision number, so it doesn't match. Log[3] evalutes to Log[3], so it ...
lericr's user avatar
  • 30.2k
9 votes

Optional argument that can be completely omitted?

I'd love to have a short syntax form for that. I'd use it more often: similarWords[string_, n:(_|PatternSequence[]) ]:= Nearest[WordList[],string,n]
Kuba's user avatar
  • 137k
9 votes
Accepted

why doesn't this work to screen for Boolean inputs?

x_Integer is a pattern which catches an x so long as the Head of ...
eyorble's user avatar
  • 9,488
9 votes

Optional pattern behaves differently in version 8, 9, and 10+

In M9 and earlier, the default value must match any pattern restrictions. In your example, the head of the default is not Integer, and so the default value could ...
Carl Woll's user avatar
  • 131k
9 votes
Accepted

changing number of arguments to a function in secondary evaluation

From my own comment: You're mixing the concepts of pure functions (# and &) with replacement (...
eyorble's user avatar
  • 9,488
9 votes

How to replace all elements of a list by a rule "element" -> "element_"

convert it to string and then deal with it ToExpression@StringJoin[#,"_"]&/@ToString/@{a,b,c,d,e} a shorter way ...
AsukaMinato's user avatar
  • 9,988
8 votes

Optional argument that can be completely omitted?

This seems to work: similarWords[string_, n: Repeated[_,{0,1}]] := Nearest[WordList[], string, n]
celtschk's user avatar
  • 19.2k
8 votes
Accepted

How can I check if a function can be written as $a * \sin (b * x + c) + d$?

Here is a differential equations/operator approach, leading to mathematics-based solutions instead of expression-matching ones. As the OP observes, matching algebraic expressions with expression ...
Michael E2's user avatar
  • 238k
8 votes
Accepted

How to divide list according to the pattern?

GatherBy[list, #[[{1, 3}]] &] {{{0, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {0, 1, 1}}, {{1, 0, 0}, {1, 1, 0}}, {{1, 0, 1}, {1, 1, 1}}} Alternatively, <...
kglr's user avatar
  • 398k
7 votes
Accepted

How do I construct a Pattern with Alternatives and PatternSequence without getting Pattern::patvar messages?

It seems like a glitch in the pattern matcher, occuring whenever named patterns appear in a head and PatternSequence appears at level 1 in the body. ...
Simon Woods's user avatar
  • 85.1k

Only top scored, non community-wiki answers of a minimum length are eligible