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

From: Antoine Noo (antoine597 at gmail.com)
Date: Fri Jul 19 12:58:35 EDT 2013

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.



2013/7/19 Joe Marshall <jmarshall at alum.mit.edu>

> You could also hide the data with a monad.
> On Jul 19, 2013 9:35 AM, "Carl Eastlund" <cce at ccs.neu.edu> wrote:
>
>> One common solution to this issue is a parameter:
>>
>> (define current-data (make-parameter #f))
>>
>> (define (f1 the-data ...)
>>   (parameterize ([current-data the-data])
>>     (f2 ...)))
>>
>> (define (f2 ...)
>>   ... (f3 ...) ...)
>>
>> ...
>>
>> (define (fx ...)
>>   ... (current-data) ...)
>>
>> The parameterize form in f1 means that calls to (current-data) will
>> produce the-data during the dynamic extend of f1 (meaning from the time f1
>> is called until it returns).  This isn't just a set! on enter and exit;
>> different threads won't see this change, for instance, and if f1 is
>> exited/re-entered using a continuation, (current-data) will only produce
>> the-data while inside f1.  So while it's not purely functional, it's a much
>> more disciplined kind of effect.
>>
>> Carl Eastlund
>>
>> On Fri, Jul 19, 2013 at 12:23 PM, Ben Duan <yfefyf at gmail.com> wrote:
>>
>>> Scenario: A piece of data is determined in the first function `f1', but
>>> is only processed in a sub-sub-sub-… function `fx'.
>>>
>>> One way is to use pass `the-data' as arguments from `f1' through `f2'
>>> all the way down to `fx':
>>>
>>>     (define f1 (the-data …)
>>>>>>       (f2 the-data …)
>>>       …)
>>>
>>>     (define f2 (the-data …)
>>>>>>       (f3 the-data …)
>>>       …)
>>>
>>>>>>
>>>     (define fx (the-data …)
>>>       … the-data …)
>>>
>>> But in the above way, the body of `f2', `f3', `f4' and so on doesn't
>>> use `the-data'. It is only passed to the next function. And I still have to
>>> add the argument `the-data'.
>>>
>>> Another way is to use `set!':
>>>
>>>     (define the-data …)
>>>
>>>     (define f1 (the-data …)
>>>>>>       (set! the-data …)
>>>>>>       (f2 …)
>>>       …)
>>>
>>>     (define f2 (…)
>>>>>>       (f3 …)
>>>       …)
>>>
>>>>>>
>>>     (define fx (…)
>>>       … the-data …)
>>>
>>> But in this way, the benefits of being functional are lost. For example
>>> there will be some problems writing tests for these functions.
>>>
>>> My question is, which way is better? Or are there other ways to solve
>>> this problem?
>>>
>>> Thanks,
>>> Ben
>>>
>>> P.S. This question is not about Racket. It's just a beginner's question
>>> about how to program. Please let me know if it's not appropriate to ask
>>> this kind of questions here. Thank you.
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>>
>>
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>>
>>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130719/d3310b59/attachment.html>

Posted on the users mailing list.