I'm studying a code with the following function declaration :

FindChain[ x:__ ,y_?FindChainQ,z:__] := { {x},y,{z} }

What is the meaning of the colon ":" after the arguments x and z ?

What would be the diference(s) with this declaration (without the colons) :

FindChain[ x__,y_?FindChainQ,z__] := { {x},y,{z} }

Thank you,

  • 1
    $\begingroup$ Check the documentation of SetDelayed. $\endgroup$
    – bbgodfrey
    Jul 27 at 17:37
  • $\begingroup$ Check the documentation for Pattern too. It says: The form s_ is equivalent to s:_ Similarly ... s__ to s:__. You can see this by doing FullForm[s__] and FullForm[s : __] . The purpose of it is a shorthand so you don't need to write the full Pattern and for when you have one symbol but a complex pattern object on the rhs. $\endgroup$
    – flinty
    Jul 28 at 12:38

It is useful when your pattern does not start with _. Suppose your pattern is a list of integers,

total[list : {__Integer}] := Total[list]
total[___] := $Failed

Or a variable that can be true or false:

func[arg:(True | False)] := If[arg, foo, bar]
func[___] := $Failed
  • $\begingroup$ I m sorry but i don t understand your first explanation. Can you develop a bit more ? $\endgroup$
    – Bendesarts
    Jul 28 at 10:58

A colon is useful to name a filter with the following syntax: name:filter. For example, polarPlot[r_, domain:{t_, tmin_, tmax_}]:=ParametricPlot[{r Cost[t], r Sin[t]}, domain] domain specifies a list of 3 éléments.

It can be useful also to factor a filter as r_Integer|r_Rational in r:(_Integer,_Rational)


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.