Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.2K Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Use multiple conditional restrictions on a argument?

Posted 3 years ago

I want to define a function with multiple conditional restrictions on its argument as follows, but failed:

f[m_Integer : Except[_List] : 0] := m

Syntax::sntxf: "f[" cannot be followed by "m_Integer:Except[_List]:0]".

Are there any tips for achieving this goal?

Regards,
Zhao

POSTED BY: Hongyi Zhao
3 Replies
Posted 3 years ago

Nice trick and neat examples. Thank you very much.

POSTED BY: Hongyi Zhao
Posted 3 years ago

Another way, without Except:

g[m : (x_Integer /; Mod[x, 7] != 0) : 0] := m
POSTED BY: Hans Milton
Posted 3 years ago

Strange example. If an expression has head Integer it cannot have head List. But I understand your question.

You could use the two argument form of Except. The second argument specifies a pattern that must be matched.

f[m : Except[x_ /; Mod[x, 7] == 0, _Integer] : 0] := m

f[15]
f[]
f[3.5]
f[14]
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard