PEP 276 Simple Iterator for ints

Skip Montanaro skip at pobox.com
Thu Nov 15 04:58:18 EST 2001


    Rainer> One thing bothers me about the Haskell syntax: using it to
    Rainer> generate sequences of less than three elements just looks wrong.

    Rainer> [1, 2, ... 3] # This is obviously [1, 2, 3].
    Rainer> [1, 2, ... 2] # What is this?  [1, 2]?
    Rainer> [1, 2, ... 1] # [1]?
    Rainer> [1, 2, ... 0] # []?

So, just don't use it for lists less than four elements... ;-)  If you are
using it in a variable context, I can see that you might not know at the
time you write the code whether the list will be < length 4:

    s = raw_input("Your name please: ")
    for i in [0, 1, ... len(s)]:
        print s[i]

I imagine the Haskell folks have solved this problem already.  My suggestion
would be that it would be equivalent to the obvious range() call:

    [0, 1, ... x]              ==>              range(0, x, 1)

    Rainer> Another thing that bothers me is that I'm it's not obvious how
    Rainer> the elements are evaluated.  This is not an issue in Haskell,
    Rainer> but it is in Python:

    Rainer> def f(n):
    Rainer>   print n
    Rainer>   return n

    Rainer> for i in [f(0), f(1), ... f(5)]: pass

    Rainer> I assume that this prints 0, 1, and 5.  That makes sense from
    Rainer> one perspective, but doesn't make sense at all from another
    Rainer> perspective.  Another example:

    Rainer> for i in [g() + 0, g() + 1, ... g() + 13]: pass

    Rainer> How often is 'g' called?  Once?  3 times?  14 times?

In the examples you listed, f() and g() would be called three times.  You
have no idea if they are idempotent or not, so you can't call them more or
less than given.  Besides, the range iterator couldn't get built until you
know what each of its three parameters are.  You have to call the functions
to build the iterator, at which point you have no notion that functions are
involved.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
It's only a 25% solution to our problems.  Of course, we only want to solve
25% of our problems, so it becomes a 100% solution.




More information about the Python-list mailing list