hongyizhao
(Hongyi Zhao)
1
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
hansmilton
(Hans Milton)
2
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]
hansmilton
(Hans Milton)
3
Another way, without Except:
g[m : (x_Integer /; Mod[x, 7] != 0) : 0] := m
hongyizhao
(Hongyi Zhao)
4
Nice trick and neat examples. Thank you very much.