[R] Continuation-parsing / trampoline / infinite recursion problem

Duncan Murdoch murdoch.duncan at gmail.com
Thu Aug 11 23:05:22 CEST 2016


On 10/08/2016 1:28 PM, Duncan Murdoch wrote:
> On 10/08/2016 1:10 PM, Thomas Mailund wrote:
>> That did the trick!
>>
>> I was so focused on not evaluating the continuation that I completely forgot that the thunk could hold an unevaluated value… now it seems to be working for all the various implementations I have been playing around with.
>>
>> I think I still need to wrap my head around *why* the forced evaluation is necessary there, but I will figure that out when my tired brain has had a little rest.
>
> The original version
>
> make_thunk <- function(f, ...) function() f(…)
>
> says to construct a new function whose body evaluates the expression
> f(...).  It never evaluates f nor ... , so they don't get evaluated
> until the first time you evaluate that new function.
>
> My version containing list(...) forces evaluation of ... .  It would
> have been even better to use
>
> make_thunk <- function(f, ...) { list(f, ...); function() f(…) }
>
> because that forces evaluation of both arguments.
>
> I suspect you would have problems with
>
> make_thunk <- function(f, ...) function() do.call(f, list(...))
>
> for exactly the same reasons as the original; I'm surprised that you
> found it appears to work.

I have done some experimentation, and am unable to reproduce the 
behaviour you described.  Using do.call() doesn't affect things.

Duncan Murdoch



More information about the R-help mailing list