Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function foobar[x_] that can produce 4 types of output

a
b
a or b
a and b

What's a good data structure, or better even, a good built-in function (head) that can represent this? I don't want to use And[] and Or[] as a, b aren't boolean. Is this a silly question?

The intended use of foobar[], or more precisely the expression it evaluates to, is to represent the type of some other expression.

My idea 1

is to use

foobar[x_] := Which[
    <condition for "a" scenario>, {{a}},
    <condition for "b" scenario>,, {{b}},
    <condition for "a or b" scenario>, {{a}, {b}},
    <condition for "a and b" scenario> {{a, b}},
]
share|improve this question
5  
The choice of data structure will depend on what you intend to do with it. In this case, I see nothing wrong with your idea... if used correctly down the road. Perhaps adding a bit more background and what you intend to do might get you better responses – rm -rf Oct 10 '12 at 17:31
2  
By using Piecewise instead of Which you get a nice TraditionalForm – belisarius Oct 10 '12 at 17:46
4  
The addendum could not have been any more vague :) More seriously, what's missing is the why. Why do you think your construct is insufficient? You probably have a reason for that — is it slow? Is it clumsy to work with? Are you having troubles using this output format?, etc. – rm -rf Oct 10 '12 at 18:02
3  
Another possible choice could be a, b, either[a,b] and both[a,b]. Or oneof[a], oneof[b], oneof[a,b], allof[a,b]. Or {1,{a}}, {1,{b}}, {1,{a,b}}, {2,{a,b}}. The last one would also allow more complex specifications like {2,{a,b,c}} (exactly 2 of a, b and c) or {2;;4, {a,b,c,d,e}} (2 to 4 out of a to e). – celtschk Oct 10 '12 at 18:19
2  
You say you don't want to use And and Or because a and b aren't Boolean -- why not? If they are expressly not Boolean then a && b or a || b will not evaluate, and this format is probably easiest to read. Do you have another reason to avoid these? – Mr.Wizard Oct 10 '12 at 18:43
show 7 more comments

closed as too localized by whuber, rm -rf Oct 27 '12 at 3:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.