racket Closures Closure with static environment


A closure is a procedure that holds internal state:

Define a procedure that returns a closure

The procedure make-an-adder takes one argument x and returns a function that closes over the value. Or to put it another way, x is within the lexical scope of the returned function.

#lang racket
(define (make-an-adder x)
  (lambda (y)
    (+ y x)))


Calling the procedure make-an-adder returns a procedure that is a closure.

Welcome to DrRacket, version 6.6 [3m].
Language: racket, with debugging; memory limit: 128 MB.
> (define 3adder (make-an-adder 3))
> (3adder 4)
> (define 8adder (make-an-adder 8))
> (8adder 4)