[plt-scheme] Lazy Evaluation

From: Jon Zeppieri (zeppieri at gmail.com)
Date: Tue Feb 9 21:56:22 EST 2010

On Tue, Feb 9, 2010 at 12:23 PM, james pack <jamestidwellpack at gmail.com>wrote:

> Hi,
> I'm trying to create a function that returns a function, here's the
> code
> (define (make-getter pattern url-string)
>  (lambda (keyword)
>    (regexp-match* pattern
>                    (get-html-page (force url-string)))))
> (define get (make-getter
>                                        #rx"<h2 class=r>([0-9]+)\""
>                                         (delay (string-append "http://
> www.google.com/custom?hl=en&sitesearchq=" keyword "&btnG=Search"))))
> (get "cat")
> What I want the make-getter function to do is return a function that
> will take an argument but since the returned function itself is
> dependent on the argument (keyword), i'm getting an error saying that
> the keyword is undefined.

Actually, the returned function, as you've written it, isn't dependent on
the argument.  make-getter is returning a lambda expression that binds, but
doesn't use, the keyword argument.  Instead, you're trying to use keyword in
a place where it isn't bound.

You probably want something like:

(define (make-getter pattern make-url-string)
 (lambda (keyword)
   (regexp-match* pattern
                   (get-html-page (make-url-string keyword)))))

(define get (make-getter
                                       #rx"<h2 class=r>([0-9]+)\""
                                        (lambda (keyword) (string-append
www.google.com/custom?hl=en&sitesearchq=" keyword "&btnG=Search"))))

> I thought using delayed evaluation would
> make the code work but it didn't.
Lazy evaluation doesn't violate lexical scope (and that's a good thing).  In
other words, *when* the expression is evaluated has nothing to do with the
binding environment in which it's evaluated.  The latter is determined by
inspecting the syntax of the program, rather than by trying to trace its
run-time behavior.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100209/46879be0/attachment.html>

Posted on the users mailing list.