LOL Booleans (21)

1 Name: #!/usr/bin/anonymous : 2008-03-27 13:57 ID:+vuM+spI

i and j are both booleans, and are TRUE. k is also a boolean, and I've just initialized it as i+j.

Should I expect k to be TRUE in some programming languages, and FALSE in others?

2 Name: #!/usr/bin/anonymous : 2008-03-27 15:11 ID:Heaven

Short answer is you shouldn't be adding booleans in the first place... but here's a summary of the behavior of various languages.

Languages in which true + true is true:

  • C: There's no boolean data type, but true is anything besides zero, and programs usually #define TRUE 1. Therefore TRUE + TRUE == 2, which is also nonzero -- and therefore also true. (but not necessarily TRUE)
  • C++: Mostly like C, but with an actual bool data type. Essentially it's just typedef enum {false, true} bool; which makes true 1 -- so again, true + true is 2.
  • Common Lisp: t + t returns t.
  • Python: True + True returns 2; Python handles anything non-empty and non-zero as True.
  • Javascript: true + true returns 2, which is a true value.
  • Perl: Like C, there's no real boolean data type. However, the canonical true value in Perl is 1, so true + true == true.
  • PHP: Again, true + true returns 2, which is also true.

Languages in which true + true raises an error

  • Ruby: true + true results in a NoMethodError. Additionally, booleans cannot be coerced into numbers (0 + true also throws an error)
  • Haskell: Same deal... you can't add to a boolean.
  • Lua: true + true results in the error "attempt to perform arithmetic on a boolean value".
  • Tcl: true and false are actually quite screwy, and personally I just use ints because it makes more sense to me, but I don't code in Tcl much anyway. However, [expr true + true] doesn't work, returning can't use non-numeric string as operand of "+".

Note that many languages in the latter category still interpret any non-zero integer value as true in an if statement, but if you're starting off with a boolean, you won't get to that point -- at least not without some amount of coercing.

So in summary, not one of the languages I have handy will evaluate TRUE + TRUE to FALSE, but several of them will result in an error of some kind. However it's still not generally a good idea to add booleans; after all, they're boolean, so you're better off using a proper boolean operator (e.g., i or j or equivalent).

3 Name: #!/usr/bin/anonymous : 2008-03-27 18:09 ID:+vuM+spI

Honestly, I can't think of any reason I would. But I have to admit to being curious. Thanks for the comprehensive answer.

4 Name: #!/usr/bin/anonymous : 2008-03-27 22:00 ID:Heaven

No problem. I have too much time on my hands.

5 Name: #!/usr/bin/anonymous : 2008-03-27 22:27 ID:Kv5U8BIj

Java raises an error as well (operator + cannot be applied to boolean,boolean), and so does Scheme.

It's also worth noting that PHP does have a real boolean type, it's just that when adding them, the variables automatically get cast to integers (with value 1) and added, and any non-zero integer (2 in this case) evaluates to true as well.
Casting the sum to a boolean and back will also give you 1, not 2.

PHP's type system is hilarious.

6 Name: #!/usr/bin/anonymous : 2008-03-27 23:01 ID:Heaven

>>5
Lots of languages have the same boolean/int semantics. Try it with javascript, python, etc. -- they'll all do the same thing.

7 Name: #!/usr/bin/anonymous : 2008-03-28 00:27 ID:ck+Erga7

I don't see the problem of summing booleans. It makes perfect sense in the context of counting. sum([x%5 == 0 for x in range(100)]) would tell you how many numbers are divisible by five in the range 0 to 99 in Python (namely, 20) for example.

8 Name: #!/usr/bin/anonymous : 2008-03-28 04:51 ID:Heaven

>>7
And that works well as long as the language has loose typing (as Python does). If not, it should be an error.

9 Name: #!/usr/bin/anonymous : 2008-03-28 12:25 ID:Heaven

boolean logic is mod 2.
In boolean logic, + is equal to ^.

10 Name: #!/usr/bin/anonymous : 2008-03-28 12:27 ID:Heaven

>>2

> C: there is no boolean data type

Incorrect, look at <stdbool.h>.

11 Name: #!/usr/bin/anonymous : 2008-03-29 05:28 ID:to4zPDh9

>>4
You also don't know Common Lisp, t is not the answer it's a type error, gb2 /prog/ and read SICP

12 Name: #!/usr/bin/anonymous : 2008-03-29 08:07 ID:Heaven

>>10
Is that C99? I haven't really done C in years.

>>11
Oh yeah. Dunno what I was thinking:

[1]> (+ t t)

*** - +: T is not a number

Regardless, shut up. T is not a "type error", it's the result of a value not enclosed in a s-expression. Also, SICP isn't even CLisp, it's Scheme, and /prog/ is full of shitheads like you who think they're awesome because they halfassed their way through a beginner-level book. So how about you go away.

13 Name: #!/usr/bin/anonymous : 2008-03-29 11:30 ID:F321LNd2

>>12

> Is that C99? I haven't really done C in years.

Yes. It was one of the IMHO useless additions in C99. I believe it was added for
more cross-compability with C++ code.. or something. Regardless, a bool type in
C is useless.


> Also, SICP isn't even CLisp, it's Scheme


Well, CLisp is not even lisp, it's a Common Lisp implementation! :-P


> /prog/ is full of shitheads like you who think they're awesome because they
> halfassed their way through a beginner-level book. So how about you go away.


Hmm.. /prog/'s majority is indeed shitheads & textboard trolls. I, in fact,
wrote a lengthy post regarding /prog/'s downfall & current issues (but not all
of them are addressed) here, if anyone is interested.


> Regardless, shut up. T is not a "type error", it's the result of a value not
> enclosed in a s-expression.


F. In fact, T is a boolean value. T for true. It's just that your common lisp
implementation likes to all-caps symbol names. t (or T) is a symbol name whose
value is `true'. t + t is, in fact three expressions (and three symbols too).
Each one is evaluated and then its value is printed in the terminal. + when
evaluated for some reason will print the previous expression. I don't know why,
but it is so. Example, (by the way, how do I in HTML?)

(+ 1 2 3) +

==> 6
==> (+ 1 2 3)


Now, let's take the three expressions evaluated, t, +, t.

t is true and evaluates to true:

t
==> T
+ prints the previous value..

+
==> T

and again, t evaluates to true

t
==> T

So there you have it. Not type error, nor the one and only output when an
expression is evaluated. (not S-expression)
Considering this comes from a /prog/ regular I suggest you don't generalize a
big user base again, for the generalization will most likely be proven false.
(notice how I first agree with you and then prove you wrong, can't make my mind
sometimes :-) Add to this, SICP is not a beginners textbook. It's quite an
interesting and challenging textbook, which I suggest every programmer. Won't
make you an expert programmer in a sudden, but there is benifit from reading the
book. (I believe, however the same can be said for any good book; So read good
books :-)


14 Name: #!/usr/bin/anonymous : 2008-03-29 16:23 ID:Heaven

ITT nerd rage.

15 Name: #!/usr/bin/anonymous : 2008-03-29 17:11 ID:Heaven

>>13

> So there you have it. Not type error, nor the one and only output when an expression is evaluated. (not S-expression)

Yeah that's pretty much what I was going for, except better-written. By "a value" I was referring to that specific value -- either 't' or '+' (which, in this case, also printed 't').

I have read SICP, and I believe it is a first-level book, so long as you understand the underlying mathematics. Not so good for students who are still trying to figure out derivatives.

Anyway, welcome to 4-ch, I'm pretty sure you'll like it here. Just do less trolling. ;)

16 Name: #!/usr/bin/anonymous : 2008-03-29 17:50 ID:ck+Erga7

>>8
What I'm trying to say is that I really think it should be like that in any language. I really don't see the problem of having true and false converted into the integers 1 and 0 as it is very helpful and not just done for hardware reasons. Another example would be the way booleans can be used for subscripting the nodes of a BST when the nodes are declared as an array of two elements in C.

17 Name: #!/usr/bin/anonymous : 2008-03-29 18:49 ID:F321LNd2

>>15

Yeah that's pretty much what I was going for, except better-written. By "a
value" I was referring to that specific value -- either 't' or '+' (which, in
this case, also printed 't').


Ah, I see. I hope my post cleared some of the potential confusion a newbie would
have.


I have read SICP, and I believe it is a first-level book, so long as you
understand the underlying mathematics. Not so good for students who are still
trying to figure out derivatives.


Say, what do you consider advanced? You're nuts, you're crazy in the coconut if
you believe SICP is for beginners. Despite it's structure, which is not very
friendly, this book, if anything, is essential to anyone who is interested in
computer science, and in programming structure. Before anyone writes complex
software he must come up with the programs structure. Abstraction is essential
for that, and the exercises while they require relatively simple tasks, they
teach that well. Understanding the mathematical symbols and their meaning is not
as importand as understanding how to write well-structured programs. phi
has nothing to do, after all, with a kernel. (just an example of some complex
application). I am still open to suggestions, so I'd like, as I mentioned again
before, to know what you consider advanced.


Anyway, welcome to 4-ch, I'm pretty sure you'll like it here. Just do less
trolling. ;)


I've been browsing 4-ch for some time now, but my posts are a-few. It's the
first time someone calls me a troll, and I'd expect that to be quite rare in an
anonymous community. As such, I'd also like you to elaborate on the matter. I
know what I am, and I'm certainly not a troll. What are the odds for a
delusionary fool whose posts are, unintentionally similar to those of a troll?
Just in case, let me repeat this once more, I am not a troll. Please do
not consider me as one, because it certainly makes me feel shitty since my posts
are genuine.

>>16


What I'm trying to say is that I really think it should be like that in any
language. I really don't see the problem of having true and false converted into
the integers 1 and 0 as it is very helpful and not just done for hardware
reasons.


I doubt a boolean data type is of any use for most languages.


Another example would be the way booleans can be used for subscripting
the nodes of a BST when the nodes are declared as an array of two elements in C.


Or, what I would do in the absence of a boolean data type is to use AND 1 if t +
t = f, or NOT NOT if t + t = t. As you see, it also depends on the semantics and
behavior of a boolean to decide which is most appropriate. Alas these semantics
might not be there, and what's for a programmer to do then? A concrete example
of rotten documentation (or absence thereof) would be Xorg. Take a look, and my
post will get a whole differend meaning to you - I am positive about this.

18 Name: #!/usr/bin/anonymous : 2008-03-29 18:50 ID:Heaven

>>17
I'm sorry, my HTML wasn't correct.

19 Name: #!/usr/bin/anonymous : 2008-03-30 06:58 ID:Heaven

>>17
"gb2 /prog/ and read SICP" is fairly standard troll rhetoric if you ask me. Notwithstanding, your other points are valid.

20 Name: #!/usr/bin/anonymous : 2008-04-16 20:09 ID:1Ah7PCBU

Maybe this wont work, but why not just set it as True when you declare it? That way you dont have to wonder about certain languages.

21 Name: #!/usr/bin/anonymous : 2008-04-23 02:27 ID:Heaven

>>20, >>1 is a specific example of a general problem

This thread has been closed. You cannot post in this thread any longer.