Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

[Caml-list] Python's yield, Lisp's call-cc or C's setjmp/longjmp in OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2003-12-17 (19:32) From: Falk Hueffner Subject: Re: [Caml-list] Python's yield, Lisp's call-cc or C's setjmp/longjmp in OCaml
```Pierre Weis <pierre.weis@inria.fr> writes:

> [...]
> > This only works for simple examples. Try for example writing a
> > function which successively yields all possible moves for a chess
> > board. The "yield" operator really helps there.
>
> Very interesting: please give us the code corresponding to this
> example, in order for us to realize how the full power of the "yield"
> operator helps to solve this problem.

I don't really know Python well (nor chess)... it would probably look
similar to:

def moves(board):
for x in range(0, 8):
for y in range(0, 8):
if board.at(x, y) == PAWN:
yield Move(x, y, x + 1)
elif board.at(x, y) == ROOK:
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
n = 0
while is_on_board(x + n*dx, y + n*dy):
yield Move(x, y, x + n*dx, y + n*dy)
n += 1
...

--
Falk

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```