guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is apply procedure not tail recursive?


From: Burton Samograd
Subject: Re: Is apply procedure not tail recursive?
Date: Wed, 16 Jan 2013 17:41:43 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Noah Lavine <address@hidden> writes:

> Hello,
>
> This is just a guess, but I think that apply is trying to put all of the
> elements of the argument list on the stack before calling +. Presumably
> when it calls +, those will all be consed up into another list, but it's
> not taking a shortcut there. So yes, it is tail recursive, but I think
> you've hit a different issue.

The problem is that scheme hash normal (eager) evaluation, so all
parameters to a function call are evaluated before applying the function
call and apply is a function.  I think the OP is expecting lazy
evalaution, not tail recursion.

> On Wed, Jan 16, 2013 at 6:34 PM, Akop Pogosian <address@hidden> wrote:
>
>> (define (zeros n)
>> ;; Make list of n zeros
>>   (define (zeros-iter n lst)
>>     (if (= n 0)
>>         lst
>>         (zeros-iter (- n 1)
>>                          (cons 0 lst))))
>>   (zeros-iter n '()))
>>
>> Results:
>>
>> guile> (apply + (zeros (expt 10 3)))
>> $2 = 0
>> guile> (apply + (zeros (expt 10 4)))
>> $3 = 0
>> guile> (apply + (zeros (expt 10 5)))
>> ERROR: Throw to key `vm-error' with args `(vm-run "VM: Stack overflow"
>> ())'.
>> guile> (apply + (zeros (expt 10 6)))
>>
>> Process scheme aborted (core dumped)
>>
>>

--
Burton Samograd




reply via email to

[Prev in Thread] Current Thread [Next in Thread]