> On Apr 20, 2017, at 10:04 PM, Lawrence Bottorff <borg...@gmail.com> wrote:
> 
> Looking at HTDP-1 
> (http://htdp.org/2003-09-26/Book/curriculum-Z-H-25.html#node_chap_19), I see 
> this:
> 
> (define (filter predicate alon)
>  (cond
>   [(empty? alon) empty]
>   [else (cond
>          [(predicate (first alon)) 
>           (cons (first alon)
>                 (filter predicate (rest alon)))]
>          [else
>           (filter predicate (rest alon))])]))
> 
> Then to find if a member of a list l is below a number i
> 
> (define (below i l) 
>  (local ((define (below-i x)
>            (> i x)))
>    (filter below-i l)))
> 
> As the proverbial beginner -- who doesn't totally understand closures -- this 
> looks like a use of the closure idea. Right? The i is "magically" known in 
> `filter`, which seems pretty closure-ish to me. Or am I totally wrong?



Here is the code in fixed width font: 

(define (below i l) 
 (local ((define (below-i x)
           (> i x)))
   (filter below-i l)))

i is valid in the entire function body of below: (local . . .) [This is called 
scope; see intermezzo]
so the i inside of the inner function definition (below-i) refers to the i in 
the outer function def. 
So when filter calls below-i, this function really knows what value i is. 

In this sense, below-i is a closure, which is a pair of two things: the 
variables it knows (and their values) PLUS the code it can run. 


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to