User loginNavigation |
Generators/Iterators and lazy evaluationHi 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 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 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? By technophobicgeek at 2007-04-04 16:20 | LtU Forum | previous forum topic | next forum topic | other blogs | 8920 reads
|
Browse archives
Active forum topics |
Recent comments
27 weeks 2 days ago
27 weeks 2 days ago
27 weeks 2 days ago
49 weeks 3 days ago
1 year 1 week ago
1 year 3 weeks ago
1 year 3 weeks ago
1 year 5 weeks ago
1 year 10 weeks ago
1 year 10 weeks ago