Python vs Ruby

Magnus Lie Hetland mlh at idi.ntnu.no
Tue Jan 30 04:34:46 EST 2001


"Greg Jorgensen" <gregj at pobox.com> wrote in message
news:954uq3$oj8$1 at nnrp1.deja.com...

> > I know. I noticed after posting.
> > (That's what you get for being cocky ;-)
>
> I hate when that happens (to me).

<wink>

> > Well... I thought my version "correctly" disallowed 0 as an argument.
>
> I wouldn't complain if you disallowed 0, but your code returns 1 for fib
> (0).

Well ... Your version "allows" negative numbers... I think one should be
able to trust programmers to use things correctly -- otherwise Python
wouldn't work anyway. (No real encapsulation etc. ;-)

> If you use the old definition where there is no fib(0), the
> function should throw an exception.

I guess. And it should perhaps be wrapped up in some sort of sequence
class... And a lot of other nice things :)

> I would use a for loop if there was a form that didn't iterate over a
> sequence when I just want to count from a to b.

Yeah. I remember some languages having things like:

repeat 10
  print "Spam"
end

That's actually quite nice, IMO. (I think both HyperTalk and PostScript
have this, at least).

But of course this is just syntactic sugar (as so much else)...

> It's my years of
> programming in C that prevents me from resisting the urge to decrement
> a loop counter down to 0.

Well... I must concede that I really dislike using a for-loop without using
the index for anything useful... (Like... for dummy in range(10)... I've
even
seen someone use the name "_" for such an index... <shudder>)

I wonder... Could we find a way of writing this that's consistent with
Python syntax? (Perhaps a future PEP?)

How about simply:

for 100:
    print "spam"

?

A new version of your little function:

def fib(n):
    a,b = 0,1
    for n:
        a,b = b,a+b
    return a

Hm. It doesn't really read well, in a way. "for x in y" is OK, I mean...
"For each x in y, do z". But -- "for n"? "For n times ..." Hm.

Next I'm sure I'll ask for standard numeric for loops without sequence
iteration... Naah...

> And of course in C a for loop IS just a while
> loop in fancy clothes.

As, indeed, it is anywhere else. And it's all just GOTO in fancy clothes,
really. (Or lambda, if you're in a theoretical mood...)

>
> > happy-to-spend-time-on-anything-useless'ly y'rs
>
> Yeah me too.

--

  Magnus Lie Hetland      (magnus at hetland dot org)

 "Reality is what refuses to disappear when you stop
  believing in it"                 -- Philip K. Dick






More information about the Python-list mailing list