This is a rather common inquiry in contests that require computing elements of a sequence.
For example, consider the following recursive implementation of the factorial function (Python):
f = lambda n:n<1or n*f(n-1)
This works well for all positive n; f(n)
returns the required result as an int.
However, f(0)
will return the result of the comparison n<1
, which is the the bool True.
Fixing this is easy enough, but it costs a few bytes:
f = lambda n:0**n or n*f(n-1)
While it is possible to think of the Booleans True and False as numbers (usually 1 and 0) and use them as such in languages such as Python, languages such as Java or Ruby do not even allow casting a Boolean to a numeric type.
Since this is such a common scenario, I think we would benefit from a default. If a challenge doesn't contemplate returning Booleans instead of numbers, should it be allowed or forbidden?
>>> isinstance(0 < 1, int)
True
When has it ever lied to us? \$\endgroup\$ – feersum Apr 29 '16 at 4:45>>> True == 1
True
\$\endgroup\$ – Cyoce Apr 29 '16 at 7:16bool
is a trivial subclass ofint
\$\endgroup\$ – cat Apr 29 '16 at 16:15==
may cast1
toTrue
. \$\endgroup\$ – Rɪᴋᴇʀ Apr 30 '16 at 1:48int()
,bool()
and etc functions are like casts, but they aren't actually casts. The fact is, True and False are numbers in Pyfon. \$\endgroup\$ – cat Apr 30 '16 at 16:131 is True
it returns false, in Python.... \$\endgroup\$ – Comrade SparklePony Mar 22 '17 at 19:41[] is []
. \$\endgroup\$ – Dennis Mar 22 '17 at 23:24True is True
returns True.... \$\endgroup\$ – Comrade SparklePony Mar 23 '17 at 14:04is
tests for reference equality. Every instance ofTrue
is the same object, but this is not necessarily true for[]
,""
,1
, etc. \$\endgroup\$ – Esolanging Fruit Oct 11 '17 at 15:41