Group Abstract Group Abstract

Message Boards Message Boards

1
|
5.6K Views
|
4 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Specify an optional boolean parameter with a default value?

Posted 4 years ago
POSTED BY: Werner Geiger
4 Replies
Posted 4 years ago

Hi Werner,

Your #3 option is close, the PatternTest has to be applied to Blank.

Clear[f];
f[x_Integer, b : (_?BooleanQ) : True] := If[b, x, -x];

{f[1], f[1, False]}
(* {1, -1} *)
POSTED BY: Rohit Namjoshi

This may be a way:

Clear[f];
f[x_Integer] := f[x, True];
f[x_Integer, b_ /; MemberQ[{True, False}, b]] := If[b, x, -x];
POSTED BY: Gianluca Gorni
Posted 4 years ago

Thanks. But this is the same as my #4

POSTED BY: Werner Geiger
Posted 4 years ago

Great, Rohit. That's it. Thanks a lot.

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard