Creating a synonymous name for a function

Discussion of Common Lisp
Post Reply
MicroVirus
Posts: 11
Joined: Sat Apr 07, 2012 6:20 am

Creating a synonymous name for a function

Post by MicroVirus » Sat Apr 07, 2012 6:32 am

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Creating a synonymous name for a function

Post by nuntius » Sat Apr 07, 2012 8:04 am

Here are a couple options.

Code: Select all

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

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: Creating a synonymous name for a function

Post by Kompottkin » Sat Apr 07, 2012 1:15 pm

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.

MicroVirus
Posts: 11
Joined: Sat Apr 07, 2012 6:20 am

Re: Creating a synonymous name for a function

Post by MicroVirus » Sun Apr 08, 2012 11:48 am

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 :)

Post Reply