nil question

B

Brian Blazer

I am a bit new to this, so please be gentle. I was wondering about the
rational of using 'nil' to represent not only 'nothing' but also
'false' in a conditional expression. In the very few other languages I
am familiar with, nil (null) is nothing wile 0 is the return for false.

Could this be an area that may cause some problems for those
inexperienced in ruby (like myself)?

Thanks,
Brian
 
J

Joao Pedrosa

Hi,

I am a bit new to this, so please be gentle. I was wondering about the
rational of using 'nil' to represent not only 'nothing' but also
'false' in a conditional expression. In the very few other languages I
am familiar with, nil (null) is nothing wile 0 is the return for false.

Could this be an area that may cause some problems for those
inexperienced in ruby (like myself)?

Maybe you will enjoy this. I like it. It's a feature for me. :)

Cheers,
Joao
 
S

Sam Roberts

Quoteing (e-mail address removed), on Sun, Jan 30, 2005 at 12:57:20PM +0900:
I am a bit new to this, so please be gentle. I was wondering about the
rational of using 'nil' to represent not only 'nothing' but also
'false' in a conditional expression. In the very few other languages I
am familiar with, nil (null) is nothing wile 0 is the return for false.

Could this be perl?

In C (exprs evaling to) zero is false, anything else is true, including
empty strings, "".

In perl, zero is false, but also the empty string is false, and don't
you get an error if you try to test undef?

In ruby, zero and empty strings are true, and nil kinda means what undef
does, except you can test it.

I *think* that in Pascal, only booleans (true/false) can be tested for
truth, numbers, ptrs, funs, these things are not allowed to be used
as if they were booleans. Don't quote me, I haven't done any pascal.

So, I'd say there isn't anything particularly the same about any of
those 4 languages.
Could this be an area that may cause some problems for those
inexperienced in ruby (like myself)?

If it was exactly like some other language (that you have
experienced), it will be unlike some other language that somebody
else has experienced.

How languages treat booleans is one of the things usually different, and
a src of much flame wars, so when moving between languages you shouldn't
assume anything, you need to ask.

Have fun,
Sam
 
W

William James

Sam Roberts wrote
In ruby, zero and empty strings are true

Since 0 is true, you should be able to do this in Ruby:

puts "yes" if -5 < x < 9

The phrase '-5 < x' should yield the value of x instead of true.
That's the way it actually works in the Icon programming language.
But we have to use the klunky

puts "yes" if -5 < x and x < 9
 
A

Austin Ziegler

Sam Roberts wrote

Since 0 is true, you should be able to do this in Ruby:

puts "yes" if -5 < x < 9

The phrase '-5 < x' should yield the value of x instead of true.
That's the way it actually works in the Icon programming language.
But we have to use the klunky

puts "yes" if -5 < x and x < 9

Please see the thread beginning at [ruby-talk:42410]. It's also
available on Google Groups from: http://qurl.net/ai

This was a question I asked over two years ago, and Hugh Sasse had
suggested some time before. I'm still of the opinion that it would be
interesting, but I'm less convinced that it's necessary. See, you can
also do:

puts "yes" if x.between?(-5, 9)
puts "yes" if (-5..9).include?(x)

-austin
 
M

Martin DeMello

Austin Ziegler said:
interesting, but I'm less convinced that it's necessary. See, you can
also do:

puts "yes" if x.between?(-5, 9)
puts "yes" if (-5..9).include?(x)

I still like

class Object
def in?(other)
other.include?(self)
end
end

puts "yes" if x.in?(-5..9)

martin
 
D

David A. Black

Hi --

Erm, say, x is -16:

(-5 < x) < 9
(-5 < -16) < 9
-5 < 9
-5

-5 is true, probably not what you want.

But -5 < -16 is not true, so it wouldn't get that far. (I assume
William means it should return x if the expression is true, false
otherwise.)


David
 
D

David A. Black

Hi --

So false is bigger than 9? Math books will need to be rewritten. :)

I assume the expression would short-circuit once one of the
sub-expressions returned false, since

x < y < z

cannot be true unless x < y. So there would never be a false < z
comparison.


David
 
E

Eric Hodel

--Apple-Mail-21--37075848
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

I assume the expression would short-circuit once one of the
sub-expressions returned false, since

x < y < z

cannot be true unless x < y. So there would never be a false < z
comparison.

Only operators can short-circuit.

You could emulate a short circuit involving a method call with the
appropriate methods on false and true...

--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-21--37075848
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFB/UxPMypVHHlsnwQRAufjAKCSAiF9GsfW7bblaXf7SKwqPkDNLwCghnS/
LM2NFKTqWkU/4eVLRA5VnW0=
=rgi0
-----END PGP SIGNATURE-----

--Apple-Mail-21--37075848--
 
D

David A. Black

Hi --

So < would get a special operator.

Please keep my beloved < as it is!

I agree. I don't want it to change -- just discussing the way the
chained version works.


David
 
W

William James

Austin Ziegler wrote
you can also do:

puts "yes" if x.between?(-5, 9)
puts "yes" if (-5..9).include?(x)

But that's not as readable as -5 < x < 9, which is how it is written
for humans in algebra.

In Icon, comparisons succeed or fail, they don't return true or false.
So it works this way (if x==0):
(-5 < 0 ) succeeds and produces 0
(0 < 9 ) succeeds and produces 9

1 < x < 9
(1 < 0) fails

-5 < x < -15
(-5 < 0 ) succeeds and produces 0
(0 < -15 ) fails

So in Icon, a < x < b means exactly what it does in algebra.

Matz and many Ruby gurus think that Ruby incorporates the best
features of preceding languages (e.g., Awk, Perl, Python, Smalltalk).
But how many of these people ever used Icon?
 
D

David A. Black

Hi --

Austin Ziegler wrote

But that's not as readable as -5 < x < 9, which is how it is written
for humans in algebra.

In Icon, comparisons succeed or fail, they don't return true or false.
So it works this way (if x==0):
(-5 < 0 ) succeeds and produces 0
(0 < 9 ) succeeds and produces 9

1 < x < 9
(1 < 0) fails

-5 < x < -15
(-5 < 0 ) succeeds and produces 0
(0 < -15 ) fails

So in Icon, a < x < b means exactly what it does in algebra.

Matz and many Ruby gurus think that Ruby incorporates the best
features of preceding languages (e.g., Awk, Perl, Python, Smalltalk).
But how many of these people ever used Icon?

Ruby does incorporate ideas from many languages, but that doesn't mean
that every existing language has a quota of space in Ruby that it gets
to fill :)

Nor is Ruby's perspective on things always filtered through other
programming languages. For example, the a < x < b idea has been
discussed before, on the question of its own merits and how it fits
into Ruby, rather than specifically because it's in Icon (or Python or
elsewhere). (See archives and this thread for more.)


David
 
A

Austin Ziegler

Austin Ziegler wrote
But that's not as readable as -5 < x < 9, which is how it is written for
humans in algebra.

I would disagree with you regarding readability. #between? is a very natural
way of reading things -- and is also enshrined in other languages (like SQL).
The advantage to the algebraic way of doing things, though, is that they can
be more accurate and variable with minor variations in syntax. Truth be told,
my alternatives aren't *quite* the same as what was presented, because they're
both inclusive.

However, I *strongly* recommend you read the chain of articles to which I
pointed you, because I used much the same argument as you're using. I was
convinced then, and remain convinced now, that there's not enough value in
this to suggest the change. (It also can result in broken code; right now, you
can do:

truth = (-5 < 0)
p truth # => true
p truth == true # # => true

If I were to do an explicit comparison of TrueClass and FalseClass against
#truth (instead of the concept of everything is true except false and nil),
then my code would break because of this change. So the second line would no
longer be useful.

-austin
 
M

Martin DeMello

David A. Black said:
Ruby does incorporate ideas from many languages, but that doesn't mean
that every existing language has a quota of space in Ruby that it gets
to fill :)

Well put :)

(Besides which, if Icon has a free space to fill, there's always suspend.)

martin
 
H

Hugh Sasse Staff Elec Eng

Austin Ziegler wrote

But that's not as readable as -5 < x < 9, which is how it is written
for humans in algebra.

In Icon, comparisons succeed or fail, they don't return true or false.

That's true for all statements in Icon. I
So in Icon, a < x < b means exactly what it does in algebra.

So effectively you get two things from a comparison:
success/failure and result-if-success

I think that you'd need to import this behaviour into Ruby -- a
rather dramatic change -- for this to work as nicely as it does in
Icon. I left my proposal on line;

the only other way I can think to do this is have the parser handle
comparison expressions at the priority it does now, but do the
grouping in the way you describe for comparisons only. Again, that
is a significant modification to the parser. That way the whole
expresion would return true or false as now and the right-hand-sides would
only be kept internally during evaluation. This layer of complexity
may hinder debugging, however.

http://www.eng.cse.dmu.ac.uk/~hgs/ruby/comparisons.html

so you can see what I wrote.
Matz and many Ruby gurus think that Ruby incorporates the best
features of preceding languages (e.g., Awk, Perl, Python, Smalltalk).
But how many of these people ever used Icon?

Icon, and now Unicon, has some excellent features, but I hope they
don't take too many of them: I've still not made the transition to
the goal directed way of thinking, and find it hard to write code
that uses it well. Suspend is nice, but Ruby has generators now.

Some search problems are particularly easy to express in a goal
directed way, so I'd like to see a module implementing that, for
such cases, but I find the present programming model easier for most
apps.Hugh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,444
Latest member
Michaeltoyler01

Latest Threads

Top