caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Generators/iterators and lazy evaluation?
@ 2007-04-04 16:33 Raj B
  2007-04-04 20:10 ` [Caml-list] " Mathias Kende
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Raj B @ 2007-04-04 16:33 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1723 bytes --]

Hi all

I've been using OCaml for a year or so building an interpreter/ 
compiler for the Python programming language as a research project.

It's worked pretty well for most part, but I'm trying to implement  
some of the features of Python involving 'lazy' features, such as  
generator functions and list comprehensions.

A generator in Python can be thought of as an arbitrarily generated  
'lazy list'. As an example
the following is a generator capable of generating powers of 2 upto a  
max value.

def pow2(max):
    start = 0
    while (start .lt. max):
       yield 2^start
       start += 1

The 'yield' statement is the point where the function returns the  
next value and suspends itself until the next time it is 'forced'. At  
that time it resumes execution where it left off.

OCaml makes this particularly hard to implement this due to lack of  
'control flow' features, including even a call/cc. The only way I can  
see right now is breaking this code down into little functions,  
saving the current execution environment during the suspend and  
keeping track of the right function to call the next time.

I've been thinking about whether I can express this in some elegant  
way in some lazy construct in OCaml, since this is basically a form  
of 'lazy' evaluation. However, I come from the C world and am not  
quite used to 'lazy' thinking :) Any ideas?

Thanks in advance
Raj

--

The Python charmer

           _                   .-=-.          .-==-.
        { }      __        .' O o '.       /  -<' )
        { }    .' O'.     / o .-. O \     /  .--v`
        { }   / .-. o\   /O  /   \  o\   /O /
         \ `-` /   \ O`-'o  /     \  O`-`o /
     jgs  `-.-`     '.____.'       `.____.'





[-- Attachment #2: Type: text/html, Size: 13792 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-04-05 20:40 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-04 16:33 Generators/iterators and lazy evaluation? Raj B
2007-04-04 20:10 ` [Caml-list] " Mathias Kende
2007-04-04 20:46 ` Alain Frisch
2007-04-04 21:16   ` Erik de Castro Lopo
2007-04-04 21:37     ` Erik de Castro Lopo
2007-04-04 22:55     ` Bill Wood
2007-04-05  2:49     ` skaller
2007-04-05 16:41     ` Richard Jones
2007-04-04 23:53 ` Jon Harrop
2007-04-05  3:13   ` Erik de Castro Lopo
2007-04-05  5:58   ` Alain Frisch
2007-04-05 20:35     ` Jon Harrop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).