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

From: Marco Morazan (morazanm at gmail.com)
Date: Fri Jul 19 19:36:32 EDT 2013

The process of lambda dropping can also solve your problem.

In essence, make f2 to fx local function definitions inside f1. This
eliminates the need for  the-data to be an explicit parameter given that f2
through fx would be within the lexical scope of the-data. This assumes, of
course, that f2 through fx are not called by other functions in your
program.

Marco



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
>
>


-- 

Cheers,

Marco

Have a´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·´ * wonderful day! :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130719/6e411302/attachment.html>

Posted on the users mailing list.