Re: Re: Boolean type-checking
- To: mathgroup at smc.vnet.net
- Subject: [mg41881] Re: Re: Boolean type-checking
- From: bobhanlon at aol.com (Bob Hanlon)
- Date: Sun, 8 Jun 2003 06:45:44 -0400 (EDT)
- References: <bbrpjk$j8b$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Clear[f];
f[x_, True] := x^2;
f[x_, False] := x^3-4;
f[3,#]& /@ {2<3, 2>3, 2<y}
{9, 23, f[3, 2 < y]}
Clear[g];
g[x_,_?(Element[#, Booleans]&)] := x^2;
g[3,#]& /@ {2<3, 2>3, 2<y}
{9, 9, g[3, 2 < y]}
Bob Hanlon
In article <bbrpjk$j8b$1 at smc.vnet.net>, Murray Eisenberg
<murray at math.umass.edu> wrote:
<< Subject: Re: Re: Boolean type-checking
From: Murray Eisenberg <murray at math.umass.edu>
To: mathgroup at smc.vnet.net
Date: Sat, 7 Jun 2003 04:27:32 +0000 (UTC)
I want one behavior when the variable takes value True; another when
that variable takes value False; and an unevaluated expression returned
when the variable takes any other value (as by improper usage of the
function).
I do NOT want one behavior when the variable takes value True and an
alternative in every other case!
And I still raise the question as to why Mathematica does not have a
built-in object for {True, False}.
Jens-Peer Kuska wrote:
> Hi,
>
> hmm, and TrueQ[] does what ?
>
> TrueQ[expr] yields True if expr is True, and yields False otherwise.
>
> why do you want to to know if expr is False if you can check
> that it is true ?
>
> Regards
> jens
>
> Murray Eisenberg wrote:
>
>>When type checking the argument to a function, it's often easy to use a
>>built-in object in a pattern, e.g.:
>>
>> f[n_Integer] := n + 1
>>
>> g[x_?Positive] := Sqrt[x]
>>
>>But what about type-checking that an argument is Boolean, i.e., True or
>>False? Of course it's easy enough to define a function to do this:
>>
>> tfQ[sym_] := MemberQ[{True, False}, sym]
>> (* or: tfQ[sym_] := Element[sym, Booleans] *)
>>
>> h[y_, flag_?tfQ] := ....
>>
>>But is there no NAMED, single built-in object that does what the
>>above-defined tfQ does?
>>
>>If I am correct that there is not, this may result from what seems to be
>>a fundamental language design decision, namely, that True and False are
>>just symbols -- they do have head Symbol -- rather than being of a
>>special type having, say head Boolean. Why was that design decision made?
>><BR><BR>