TransWikia.com

Simplest way to check if optional parameter is omitted?

Mathematica Asked on March 21, 2021

Suppose I have a function

f[x_,y___]:= (code)

and I want my code to check whether the parameter y is omitted. What’s the easiest way to do this?

I tried some quick guesses, like If[y == Null] and If[¬ValueQ@y], but these didn’t work. I was able to use

f[x_,y___]:=
Module[{yList},
yList={y};
If[yList == {},(* code *)]
]

but surely there is a more elegant method than this clunky one.

One Answer

The easy way is to use two different patterns:

f[x_]:= single arg code
f[x_,y__]:= double arg code

Correct answer by John Doty on March 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP