[racket] Use `set!' or not in this [other] scenario?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Jul 19 13:30:58 EDT 2013


I'm changing the subject slightly to make a second thread, just to avoid
confusion between the two set! questions.

I'm not sure I entirely understand why you're using set! to produce your
results rather than simply returning them from the function.  I believe
you're saying it's because the function isn't tail recursive, but non-tail
recursion will be equally as (in)efficient either way.

There are certainly some cases where it's more natural to use set! to
accumulate a result than to build it up recursively, but they're the
exception rather than the rule.  I suggest trying to write your solution
functionally first, and see how that goes.  Resort to set! only if that
fails somehow.

Carl Eastlund

On Fri, Jul 19, 2013 at 12:58 PM, Antoine Noo <antoine597 at gmail.com> wrote:

> Hello,
> as an extension of the question of Ben.
> (define (func args)
>   (define result init-data)
>   (define (loop args-loop)
>     ...
>     (set! result some-data)
>     ....)
>   (loop args)
>   result)
> Is this a good practice?
> And if you ask why don't directly return loop, it is for the case to make
> recursive call every where in the function not only in terminal possition.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130719/9faedd54/attachment.html>

Posted on the users mailing list.