From: Raj B <rajb@rice.edu>
To: caml-list@yquem.inria.fr
Subject: Generators/iterators and lazy evaluation?
Date: Wed, 4 Apr 2007 11:33:18 -0500 [thread overview]
Message-ID: <CCF54B6E-E0E6-48B3-93A2-458FFF5FD769@rice.edu> (raw)
[-- 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 --]
next reply other threads:[~2007-04-04 16:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-04 16:33 Raj B [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CCF54B6E-E0E6-48B3-93A2-458FFF5FD769@rice.edu \
--to=rajb@rice.edu \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox