tags:

views:

49

answers:

2

In Common Lisp, is it possible to redefine an already defined function within a certain scope? For example, given a function A that calls a function B. Can I temporarily redefine B during a call to A?

I'm looking for something along the lines of a let block, but that can redefine functions.

+5  A: 

Local functions can be introduced with FLET and LABELS.

Rainer Joswig
+3  A: 

Within a given lexical scope, yes. Use FLET or LABELS. Any function defined with FLET will be unable to call functions defined in the same lexical scope, if you want that (for, say, self-recursive of a group of mutually recursive functions), you will need to use LABELS.

Note that both FLET and LABELS only establish lexical shadowing, should not be used to shadow functions from the COMMON-LISP package and will not dynamically change what function is called from outside the lexical scope the form establishes.

Vatine
So, since it's lexically scoped it will only affect the actual body of the flet/labels and not the whole funcion call hierarchy, so to speak?
Erik Öjebo
EXactly so, it will only "exist" within the lexcial scope, not making a blind bit of difference to other functions. There is, as far as I am aware, not any way of dynamically bind symbol-to-function mappings.
Vatine