This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Creating a synonymous name for a function

4 posts · 2474 views

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

Here are a couple options.
(labels ((cow () "cow"))
  (macrolet ((chicken () '(cow)))
    (symbol-macrolet ((dog (cow)))
      (list (cow) (chicken) dog))))

Re: Creating a synonymous name for a function

You can always use apply, of course, but it probably won't exactly improve the stack traces you see in the debugger.
(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

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