I'm working through the early examples of the "Stratified Design" paper[1] and I've run (almost immediately!) into something that doesn't seem to work.

They describe an average-damp function that given a function, returns a new function that averages the value with the result of the function:

- Code: Select all
`(define (average)`

(div (apply 'add (args)) (length (args))))

(define (average-damp f)

(fn (x)

(average x (f x))))

You might use it like this: ((average-damp (fn (y) (+ 2 y))) 3)

And expect to get 4 (the average of 3 and the result of adding 2 to 3). However, instead I get this:

- Code: Select all
`ERR: invalid function : (f x)`

called from user defined function average

I assume that this is happening because the function f, that is passed to average-damp is not lexically closed in the new function that's returned, because NewLisp is not doing that sort of thing.

I have looked through the "Functions as Data" section of the Code Patterns and those examples didn't seem to help. I tried both "expand" and "letex" and they didn't seem to help.

So, is it possible to implement something like average-damp in NewLisp, and how would you do it?

Thanks!

[1] See http://dspace.mit.edu/bitstream/handle/ ... sequence=2