[plt-scheme] Newbie Lambda( ) (???????????)
Thanks I understand now, this is new to me. My usual functions only
return values, never could imagine that a function could return a
function, I was doing some research and this is called higher order
functions or I am in a mistake?.
El 13/02/2009, a las 12:58 p.m., Stephen Bloch escribió:
>
> On Feb 13, 2009, at 8:46 AM, Jesus Boadas wrote:
>
>> I am a very very newbie in functional and scheme programming, Im an
>> imperative programmer since 1989, following the tutorials in the
>> Plt-Scheme I cant get a clue in this:
>>
>> (define (serve port-no)
>> (define listener (tcp-listen port-no 5 #t))
>> (define (loop)
>> (accept-and-handle listener)
>> (loop))
>> (define t (thread loop))
>> (lambda ()
>> (kill-thread t)
>> (tcp-close listener)))
>>
>> and
>>
>> > (define stop (serve 8081))
>>
>>
>> I simply cant see the relation between stop and lambda(), lambda()
>> is inside the function serve and stop is outside serve, where is
>> the logic of this ?
>
> The neat idea of this example, one which you may not have
> encountered outside functional programming, is that a function can
> take in and return not only ordinary data types, but functions.
> This one in particular returns a function as its value.
>
> A simpler example:
> (define (make-adder n)
> (lambda (m) (+ m n)))
> (define add3 (make-adder 3))
> (add3 12) "should be 15"
> (map add3 (list 1 2 3 4 5)) "should be (list 4 5 6 7 8)"
>
> (define (add-to-each num nums)
> (map (make-adder num) nums))
> (add-to-each 5 (list 10 20 30 40 50)) "should be (list 15 25 35 45
> 55)"
>
> Make sure you understand why this all works, and then the client/
> server example will make more sense.
>
>
> Stephen Bloch
> sbloch at adelphi.edu
>