Creating a synonymous name for a function

Discussion of Common Lisp

Creating a synonymous name for a function

Postby 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
MicroVirus
 
Posts: 11
Joined: Sat Apr 07, 2012 6:20 am

Re: Creating a synonymous name for a function

Postby 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))))
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Creating a synonymous name for a function

Postby 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.
User avatar
Kompottkin
 
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany

Re: Creating a synonymous name for a function

Postby 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 :)
MicroVirus
 
Posts: 11
Joined: Sat Apr 07, 2012 6:20 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests