[racket] Call-Site Code Replacement
Hi,
Thank you for the solution. So in your solution,
the call-site is:
... (f a) ...
^ here
I was more thinking of a call-site where f can be variable.
So I was more thinking of a call site:
... (f a) ...
^ here
But you are right, with a destructive assignment,
we can do a lot.
Will this still count as a solution in a purely
functional language?
Bye
Jay McCarthy schrieb:
> Only this particular call site? Because in general, it would be simple to do:
>
> #lang racket/base
>
> (define (f a)
> (printf "(f ~a)\n" a)
> (set! the-f g)
> 0)
>
> (define the-f f)
>
> (define (g a)
> (printf "(g ~a)\n" a)
> (set! the-f f)
> 1)
>
> (module+ main
> (for ([i (in-range 10)])
> (the-f i)))
>
> OUTPUT:
>
> (f 0)
> (g 1)
> (f 2)
> (g 3)
> (f 4)
> (g 5)
> (f 6)
> (g 7)
> (f 8)
> (g 9)
>
>
> On Mon, Oct 22, 2012 at 11:55 AM, Jan Burse <janburse at fastmail.fm> wrote:
>> Dear All,
>>
>> I just wonder whether it is possible in a functional
>> language to do call site replacement. I envision this
>> as a special form of lazyness.
>>
>> Basically I would call somewhere a function f
>> with an argument a:
>>
>> ... (f a) ...
>>
>> The function would then manage to replace itself
>> by a function g with an argument b:
>>
>> ... (g b) ...
>>
>> So that the next time the original call site of (f a)
>> is invoked in fact (g b) is invoked.
>>
>> Hints welcome.
>>
>> Bye
>>
>> ____________________
>> Racket Users list:
>> http://lists.racket-lang.org/users
>
>
>