avp
avp

Reputation: 4925

Is it possible to have an alias for the function name in Lisp?

...just like packages do.

I use Emacs (maybe, it can offer some kind of solution).

For example (defun the-very-very-long-but-good-name () ...) is not to useful later in code. But the name like Fn-15 or the first letters abbreviation is not useful too. Is it possible either to have an alias like for packages or to access the documentation string while trying to recall the function's name?

In other words, is it possible for functions to mix somehow self-documenting and short names?

Upvotes: 19

Views: 6133

Answers (8)

muyinliu
muyinliu

Reputation: 91

from 《On Lisp》?Here is the code:

(defmacro alias (new-name prev-name)
  `(defmacro ,new-name (&rest args)
     `(,',prev-name ,@args)))

; use: (alias df defun)


(defun group (source n)
  (if (zerop n) (error "zero length"))
  (labels ((rec (source acc)
             (let ((rest (nthcdr n source)))
               (if (consp rest)
                   (rec rest (cons (subseq source 0 n) acc))
                   (nreverse (cons source acc))))))
    (if source (rec source nil) nil)))

(defmacro aliasx (&rest names)
  `(alias
     ,@(mapcar #'(lambda (pair)
                   `(alias ,@pair))
               (group names 2))))

; use: (aliasx df1 defun 
;              df2 defun 
;              df3 defun)

Upvotes: 5

Thayne
Thayne

Reputation: 7042

You could use setf to assign the function to the function cell of another, for example:

(defmacro alias (new-name prev-name)
  `(setf (symbol-function ,new-name) (symbol-function ,prev-name))) 

Upvotes: 5

Allen
Allen

Reputation: 5120

You want defalias. (defalias 'newname 'oldname) will preserve documentation and even show "newname is an alias for `oldname'" when its documentation is requested.

Upvotes: 48

Alex Coventry
Alex Coventry

Reputation: 70967

If it's all the typing which makes continual use of long names undesirable, then yes, emacs can help. Check out abbrev-mode. Also well thought-of in this context is hippie-expand.

If it's a question of readability, that's harder.

Upvotes: 4

dsm
dsm

Reputation: 10395

you can use (defmacro ...) to alias a function

Upvotes: -1

leppie
leppie

Reputation: 117350

I dont know Emacs, but wouldn't (define shortname longnamefunctionblahblah) work?

Upvotes: 0

Marcin
Marcin

Reputation: 49886

You could simply have a function that just calls another function.

Upvotes: -1

David M. Karr
David M. Karr

Reputation: 15235

If your problem is that you can't remember a very long function name, but you remember PART of the name, that's what "apropos" is for. In my Emacs, I have "C-h a" bound to "hyper-apropos". You enter a substring of the symbol you're looking for, and it lists all the matches.

Upvotes: 0

Related Questions