On Nov 14, 2008, at 7:05 AM, Jason Grout wrote:

>
> Mike Hansen wrote:
>> Hello,
>>
>> On Nov 14, 12:26 am, Jason Grout <[EMAIL PROTECTED]> wrote:
>>> Franco Saliola wrote:
>>>
>>>>> Any comments or objections?
>>>> What about list comprehensions? Something like the following.
>>>> sage: var('i,n')
>>>> (i, n)
>>>> sage: sum(2^i for i in range(n+1))
>>>> 2^(n+1) - 1
>>>> I ask because this seems like the natural/first thing a user  
>>>> would try.
>>> It's consistent:
>>>
>>> sage: sum(i for i in range(10))
>>> 45
>>>
>>> However, range would have to also be overridden here, since range  
>>> only
>>> accepts integers.
>>>
>>> Or we could override the ellipsis notation:
>>>
>>> sum(i for i in (1..oo))
>>>
>>> sum(i for i in (1..n+1))
>>
>> I don't think any of these are even feasible or what we want since
>> they create a "black box" generator which gets passed into sum.
>
>
> I agree that they probably aren't feasible.  I was throwing them  
> out for
> the purposes of brainstorming for interfaces.

Given that they're by far the most natural interface, we shouldn't  
rule them out for sure. Consider

sage: import dis
sage: g = (i for i in range(10) if i % 3r)
sage: dis.dis(foo(10).gi_frame.f_code)
   2           0 SETUP_LOOP              29 (to 32)
               3 LOAD_GLOBAL              0 (range)
               6 LOAD_FAST                0 (n)
               9 CALL_FUNCTION            1
              12 GET_ITER
         >>   13 FOR_ITER                15 (to 31)
              16 STORE_FAST               1 (i)

   3          19 LOAD_FAST                1 (i)
              22 LOAD_FAST                1 (i)
              25 BINARY_MULTIPLY
              26 YIELD_VALUE
              27 POP_TOP
              28 JUMP_ABSOLUTE           13
         >>   31 POP_BLOCK
         >>   32 LOAD_CONST               0 (None)
              35 RETURN_VALUE

If we evaluate the conditional (12-19) with the loop variable set to  
a symbolic we get the condition, and we evaluate the expression  
(22-25) at a symbolic loop variable to get the generic term. The  
inner iterator is local, and we can recognize range, xrange, and  
recursively attempt to descend for others. Of course the [a..b]  
notation would have to be extended to support symbolic endpoints, and  
one would have to be careful to not botch up arbitrary generators.

- Robert


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to