Suppose that I want to have a function that is evaluated only when a user-specific, predefined flag is given out of a list of possible flags. For example
Clear[flags];
flags = {fyes, fno};
Clear[testf];
testf[flag_:flags] :=
If[flag === fyes, Print["yes"], Print["no"]];
I am aware that the above snippet is wrong but that's the closest that I could get.
testf
should be evaluated only if flag
is either fyes
or fno
and stay unevaluated for any other argument.
How is this possible to be achieved?
testf[flag_ /; MemberQ[flags, flag]] := If[flag === fyes, Print["yes"], Print["no"]];
$\endgroup$testf[flag_: Alternatives @@ flags] := ...
? $\endgroup$