guile-user
[Top][All Lists]
Advanced

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

Re: why I love scheme


From: Zelphir Kaltstahl
Subject: Re: why I love scheme
Date: Wed, 15 Dec 2021 09:38:41 +0000

Hello Stefan!

This translates a recursive tree function into a tail recursive function.
However, in this case, I am not sure it is really worth doing, in comparison to
the naive (+ first-branch other-branch) solution. The reason is, that instead of
a call stack for multiple branches, you are only moving that stuff into a longer
and longer continuation, which will be on the stack in each recursive call.

However, I think you or other people on the list probably know more about this
than I do and about how the approaches compare in terms of memory and time.
Maybe the stack frames are more in size than the memory consumed by the overhead
of the continuation, or the other way around.

Regards,
Zelphir

On 12/15/21 12:44 AM, Stefan Israelsson Tampe wrote:
> Maybe you think the below program is trivial, but I adore named let's so
> much that I just cannot fathom that when people go functional they totally
> miss this beauty
>
>
> (define (count tree)
>
> ;; s = total sum up to now
>
> ;; t = tree of the type (car = child . cdr = siblings)
>
> ;; cont is the continuation, (cont 10) will continue
>
> ;; the calculation with the sum=10 see how we initiate
>
> ;; with a continuation that evaluates returns it's argument
>
>
> (let loop ((s 0) (t tree) (cont (lambda (s) s)))
>
> (if (pair? t)
>
> (loop s (car t) (lambda (s) (loop s (cdr t) cont)))
>
> (cont (if (number? t) t 0))))

-- 
repositories: https://notabug.org/ZelphirKaltstahl




reply via email to

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