[racket] Dynamically Bind Functions

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Fri Dec 21 21:45:08 EST 2012

On 12/21/12 9:41 PM, Cristian Esquivias wrote:
> I'm trying to replace a function with another using parameterize.
>
> For example, when I try:
>
> (define (add2 n)
>    (add1 (add1 n)))
>
> (parameterize ([add1 (λ [n] (+ n 2))])
>    (add2 2))
>
> I get an error:
>
>   parameterize: contract violation
>    expected: parameter?
>    received: #<procedure:add1>
>
> How do I re-bind functions in Racket?

You can only use parameterize with names bound to parameter values. 
Here's an example:

#lang racket
(define padd1 (make-parameter add1))

((padd1) 4)

(parameterize ([padd1 (λ (n) (+ 2 n))])
   ((padd1) 4))

David




Posted on the users mailing list.