I don't think I sent this to the group last time. Something odd still about how I interact with this group vs. the other ones I'm in:<br><div class="gmail_quote">-------------------------------------------------------<br>
<br>ok, I am new, too, to fp, so, perhaps partially to help me learn ... I was somewhat confused by the stop example, at first, also.<br><br>I initially thought that once you defined "stop" one time, you were set forever. I was sort of assuming symmetry between "server" and "stop". . . . like, after the definition, you could just call (stop) after every call to (server 8081). But the example was adamant that we had somehow "fixed" the problem of not shutting down the listener. So I started thinking it through, like, "what I really need is a handle to something, like the thread . .. yeah the ACTUAL one that the listener is using . . . .so I can kill it". And that was it. I understood enough to know that what's needed is to <u>redefine stop every time you start the server</u>. That's the part I didn't grok right away. You're not making a reusable function called "(stop)" the way you are making a reusable function "(server)".<br>
<br>Not only CAN you return a function, the REASON you are in this case is so that its execution will be tied (bound) to the thread that was started. I am thinking this is an example of a "closure".<br><br>I guess in OO, you'd make a server "instance", and it would have a "stop" command . . .. or you could "delete" it (in C++), which would call the "destructor" . . . or in Java, there's some what of an opportunity in "finalize()", but not a reliable one. In Python, I'm thinking you'd have a "stop", and it'd be obvious that construction and "stopping" aren't symmetric operations.<br>
<br>Now that I've made this comparison, it seems like the argument you have for fp reduces from "you can't do this in OO" to, "you need more boilerplate and fanfare to do it in OO" (possibly a whole file, possibly two -- one for the header and one for the implementation, RAII semantics on the constructor, etc).<br>
<br>--------------------<br>Then Macro said:<br><br><br>
Yes, you are on the right track. It would be a lot more verbose in OO.<br>
There are some things that are done better by an OO language (e.g.<br>
encapsulation and polymorphic dispatch). Others, however, are<br>
incredibly hard to do (and should not as demonstrated by FP).<br>
<br>
--<br>
<br>
Cheers,<br>
<font color="#888888"><br>
Marco</font><br><br><div><div></div><div class="Wj3C7c"><br>
<br><div class="gmail_quote">On Fri, Feb 13, 2009 at 5:03 PM, Marco Morazan <span dir="ltr"><<a href="mailto:morazanm@gmail.com" target="_blank">morazanm@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>On Fri, Feb 13, 2009 at 1:35 PM, Jesus Boadas <<a href="mailto:jboadas@gmail.com" target="_blank">jboadas@gmail.com</a>> wrote:<br>
</div><div>> Thanks I understand now, this is new to me. My usual functions only return<br>
> values, never could imagine that a function could return a function, I was<br>
> doing some research and this is called higher order functions or I am in a<br>
> mistake?.<br>
><br>
<br>
</div>No, you are not. You are quite right. It is quite elegant, no? :-)<br>
<br>
Consider, now, a function, f, that returns a function as its value<br>
after being applied to a set of arguments. A nice way to think of such<br>
a function is as specialized-function generator. For example, consider<br>
the problem of creating functions (i.e. what you do when you write<br>
functions) that return a list with the results of adding a constant to<br>
every element of a list of numbers. You would like to write a function<br>
that takes as input a number (i.e. the constant to add) and that<br>
returns a function that takes as input a list of numbers and that<br>
returns a list by adding the given constant to every element of the<br>
list. The code for this could look something like this:<br>
<br>
; make-list-adder: number --> (list-of-numbers --> list-of-numbers)<br>
(define (make-list-adder n)<br>
; add: number --> number<br>
(define (add m) (+ n m))<br>
<br>
; add-n-to-list: list-of-numbers --> list-of-numbers<br>
(define (add-n-to-list lon) (map add lon))<br>
<br>
add-n-to-list)<br>
<br>
Every time make-list-adder is applied to an argument it returns a<br>
function for which n is fixed. We can now, for example, define<br>
functions to add 1 and to add 5 to every element of a list of numbers<br>
without having to explicitly write code (in the conventional sense):<br>
<br>
(define add-1-to-list (make-list-adder 1))<br>
<br>
(define add-5-to-list (make-list-adder 5))<br>
<br>
The above definitions should illustrate that we have not written the<br>
code for neither add-1-to-list nor add-5-to-list in the conventional<br>
sense, but yet we have functions we can use!<br>
<br>
(add-1-to-list (list 1 2 3)) = (list 2 3 4)<br>
<br>
(add-5-to-list (list 1 2 3)) = (list 6 7 8)<br>
<br>
By writing one abstract function, we have the power to define a whole<br>
class of functions. I hope this gives you an appetite to learn more<br>
about HOFs.<br>
<br>
--<br>
<br>
Cheers,<br>
<font color="#888888"><br>
Marco<br>
</font><div><div></div><div>_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>
</div></div></blockquote></div><br>
</div></div></div><br>