On Thu, 03 Jun 2004 16:44:34 -0400, "Slava Pestov" <

slava@...>

said:

> In practice, I avoid writing functions that return new functions.

Why is that? Or are functions that 'capture state' an example of the type

of thing that needs to return new functions? I wrote a 'generator'

function in Factor to traverse a tree. Given a tree calling the generator

function will return a function that when called will return the leaves

of the tree in order:

[ 1 2 [ 3 4 ] 5 ] tree->generator

=> a-function

call .

=> 1

call .

=> 2

call .

=> 3

call .

=> 4

call .

=> 5

I did this by having the generated function return the continuation to

call to resume the function and the value of the tree. I couldn't think

of a way to do this without having the function value returned. Any

thoughts on a better interface for the generator function?

BTW, this was working through a Scheme exercise, translating it to

Factor. It's the 'Same Fringe' problem:

http://c2.com/cgi/wiki?SameFringeProblem
My basic example without continuations flattened the trees and compared

for equality. The idea for this one was to use co-routines to go through

the trees one by one and return on the first in-equality, avoiding the

need to cons up and flatten the tree.

Chris.

--

Chris Double

chris.double@...