I'm working on learning Mathematica programming for personal edification and am trying to find a more elegant way to do a matching expression which will match only a list containing elements that are three types, in this case a (sub)list (of any type), integer or string. (It is similar to a problem from chapter 4 of Wellin.)
I came up with this answer, using a helper function, but it doesn't seem very elegant. I couldn't figure out a way to do this without a helper function after trying a bunch of syntactical variants.
exp2 = {4, {a, b}, "g"}
integerListStringQ[x_] := IntegerQ[x] || ListQ[x] || StringQ[x]
MatchQ[exp2, {__?integerListStringQ}]
MatchQ[{a, 1, "a"}, {__?integerListStringQ}]
MatchQ[{}, {__?integerListStringQ}]
Expected results: True, False, False. In any event, what I would like is something that looks more like this, perhaps? (Expecting a "True" answer.)
MatchQ[exp2, {__?IntegerQ|ListQ|StringQ}]
MatchQ[exp2, {__?Integer|List|String}]
Anyway, I don't know, but these construction I did above don't work, and the solution I did that does work seems inelegant. I feel that I shouldn't need a helper function for something this basic in a functional language.
Thanks!