[plt-scheme] Newbie Schemer question
> I would like to execute a function that has one of its variable
> defined
> outside this function, in an upper context. Suppose that I define a
> simple
> function such as:
>
> (define (f n)
> (+ *p* n))
>
> I would like to write something like that (the following syntax is
> incorrect):
>
> ((let ((*p* 1))
> (lambda ()
> (f 2)))
>
> How should I formulate this in Scheme?
Right, your suggestion won't work because the *p* introduced by the
let-binding is not in scope where f is defined.
How about:
(define f
(let ([*p* 1])
(lambda (n)
(+ *p* n))))
Since *p* is fixed, you could just inline its value:
(define (f n)
(+ 1 n))
which is more idiomatically written as
(define f add1)
-- Paul