2
$\begingroup$

This works:

f[pattern : {_Integer ..} | Verbatim[Except]@{_Integer ..}] := "sneed"
{f[{1, 2, 3}], f[Except@{1, 2, 3}]}
(* {"sneed", "sneed"} *)

But what if instead of {_Integer..} it's a longer expression that I'd rather not repeat?

This also works:

g[pattern : Identity@{_Integer ..}] := "sneed"
g[{1, 2, 3}]
(* "sneed" *)

So I figured this should do the trick:

h[pattern : (Identity | Verbatim[Except])@{_Integer ..}] := "sneed"
{h[{1, 2, 3}], h[Except@{1, 2, 3}]}
(* {h[{1, 2, 3}], "sneed"} *)

Why does one of them not evaluate? How can I make it work?

$\endgroup$
2
  • 2
    $\begingroup$ You could do this: h[pattern : # | Verbatim[Except][#] &@{_Integer ..}] := "sneed", which would still expand and repeat things at definition-time, expanding the definition, but at least you don't have to do that manually. $\endgroup$ Jun 21 at 20:08
  • 2
    $\begingroup$ Similar to @LeonidShifrin's recommendation: With[{complex = {_Integer ..}}, h[pattern : complex | Verbatim[Except][complex]] := "sneed"] $\endgroup$ Jun 21 at 20:10

Your Answer

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

Browse other questions tagged or ask your own question.