<div dir="ltr">I think more context would be useful to know why a specific case is useful. But, I'll do my best with none...<div><br></div><div>By using functions like this, you can control when things happen and what they can see. For instance...</div>
<div><br></div><div><div>(define (make-worker)</div><div> (define a (build-list 10 add1)</div><div> (define (worker)</div><div> ;; a is visible to 'worker'</div><div> (+ (first a) 10))</div><div> worker)</div>
<div><br></div><div>;; a is not visible here</div><div><br></div><div>;; .... you do some other work ...</div><div><br></div><div>;; and only now do you run the construction of a</div><div><br></div><div>(define some-worker</div>
<div> (make-worker))</div><div><br></div><div>;; .... you do some other work before getting the contents</div><div><br></div><div>(define answer</div><div> (some-worker))</div></div><div><br></div><div>This is all because closures are a basic form of objects---their environment values are their "private fields" and "apply" is their one method. Thus, any time that you would find OO useful, you might find a pattern like this useful. Especially when you don't want to go whole hog and use the object/class system from racket/class</div>
<div><br></div><div>Jay</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Sep 9, 2013 at 8:24 PM, Chad A. <span dir="ltr"><<a href="mailto:chad@neomantic.com" target="_blank">chad@neomantic.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm wondering if someone can help me understand a design-pattern that<br>
I have noticed in the Racket code base. (I'm teaching myself, so<br>
review other people's code alot).<br>
<br>
I've seen this pattern..which is a bit contrived.<br>
<br>
(define (say-hello)<br>
(define (display-hello)<br>
(display "hello")))<br>
<br>
So this produces a producer, and to evaluate it I would need to write<br>
((hello)) => "hello"<br>
<br>
But then I see this...<br>
<br>
(define say-hi (say-hello)). When I evaluate (say-hello), I get<br>
"hello". Obviously, the definition of "say-hi" contains the evaluated<br>
(say-hello) than returns the 'display-hello' procedure to be<br>
evaluated.<br>
<br>
My question is...why would I want to write the code like this? 3<br>
definitions have been created which could easily be compressed into<br>
one.<br>
<br>
(I also see nothing in the code I'm looking at that appears to<br>
capture a binding via a closure).<br>
<br>
Thanks in advance for any insights someone can share.<br>
<br>
Chad<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Jay McCarthy <<a href="mailto:jay@cs.byu.edu" target="_blank">jay@cs.byu.edu</a>><br>Assistant Professor / Brigham Young University<br><a href="http://faculty.cs.byu.edu/~jay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
<br>"The glory of God is Intelligence" - D&C 93
</div>