Page 1 of 1

Creating a synonymous name for a function

Posted: Sat Apr 07, 2012 6:32 am
by MicroVirus
Dear Lispers,

Is it possibly to easily bind an existing function to a symbol, in such a way that that the symbol can be used synonymously with the original function/symbol, specifically for the lexical environment?
What I'd want to do is this:

Code: Select all

(labels ((cow () "cow"))
  (flet ((chicken #'cow))
    (list (cow) (chicken))))

--> ("cow" "cow")
So basically, for the code block inside the chicken binding, I want chicken to be synonymous to cow. I've looked up symbol-function, and it looked promising, but from what I understood it's only for the global bindings and not for lexical function-bindings as created by labels. Does anyone know if this can be done (my gut feeling says it should be possible, somehow ;) )? My goal is to create a small macro block where certain helper-functions are bound and usable (and with one that should have two names). I tried searching for this, but haven't been able to find this question asked elsewhere.

Best Regards,

Richard

Re: Creating a synonymous name for a function

Posted: Sat Apr 07, 2012 8:04 am
by nuntius
Here are a couple options.

Code: Select all

(labels ((cow () "cow"))
  (macrolet ((chicken () '(cow)))
    (symbol-macrolet ((dog (cow)))
      (list (cow) (chicken) dog))))

Re: Creating a synonymous name for a function

Posted: Sat Apr 07, 2012 1:15 pm
by Kompottkin
You can always use apply, of course, but it probably won't exactly improve the stack traces you see in the debugger.

Code: Select all

(flet ((chicken (&rest args) (apply #'cow args)))
  ...)
If you want to portably do better than that, you might have to do code walking.

Re: Creating a synonymous name for a function

Posted: Sun Apr 08, 2012 11:48 am
by MicroVirus
Thanks for the responses. I got a bit confused in all the possibilities, and what I ended up needing was a macrolet, as I also need to pass the parameters directly to the other function (which contains required and keyword parameters):

Code: Select all

(labels ((cow (some arguments here) "cow"))
  (macrolet ((chicken (&rest args)
                      `(cow ,@args)))
     ;; Do stuff here
   ))
Again, thanks for the replies, which helped clear up my head :)