1

I would like to set aliases in common lisp(clisp to be exact) for commands that are used a lot, such as "defun" and "lambda" etc, is it possible to do this?

This is actually kind of a duplicate of this question, but I can not comment and the solution does not work for defun or lambda in both sbcl and clisp

Community
  • 1
  • 1
Kyuvi
  • 360
  • 3
  • 13

2 Answers2

6

Macros:

CL-USER 5 > (setf (macro-function 'dm) (macro-function 'defmethod))
#<Function DEFMETHOD 410009A014>

CL-USER 6 > (dm m1+ ((v vector)) (map 'vector #'1+ v))
#<STANDARD-METHOD M1+ NIL (VECTOR) 4130003913>

CL-USER 7 > (m1+ #(1 2 3 4))
#(2 3 4 5)
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
3

The whole point by macros is to provide a source rewriting service.. Thus I want to give you this and you can make that out of it:

(defmacro df (name (&rest arguments) &body body)
  `(defun ,name ,arguments ,@body))

(df test (x) (+ x x))
(test 5) ; ==> 10

We have just shortened the name.. Lets make another one:

(defmacro df1 (name &body body)
  `(defun ,name (_) ,@body))

(df1 test (+ _ _))
(test 5) ; ==> 10

And so on...

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • 1
    The use of `:` alone is [undefined according to the standard](http://www.lispworks.com/documentation/HyperSpec/Body/02_ce.htm). It seems ABCL, CLisp and ECL accept it as a symbol which name is `""`; ACL, CCL, LW and SBCL don't accept that syntax. – acelent Mar 27 '17 at 14:27
  • @acelent It makes sense that `:` is special in CL. I've chosen something else. – Sylwester Mar 27 '17 at 18:20
  • This is a bit more complicated than I was looking for, as I am still to understand the terminology and macros in general. Any idea how this would translate to (a dialect of ) scheme? – Kyuvi Apr 03 '17 at 19:56
  • @DiiP A macro is like a function that takes unevaluated code as arguments. `(df1 test (+ _ _))` first calls the macro function with `name` as the symbol `test` and body as the list `((+ _ _))` the macro function then produces the data structure `(defun test (_) (+ _ _))` and that is the actual code that Common Lisp gets to evaluate. Scheme macros, except syntax-rules, are a bit more complex. – Sylwester Apr 03 '17 at 20:10