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

三世轮回 提交于 2019-12-03 03:35:24
Allen

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

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

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)

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.

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.

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!