Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defparameter vs defun for passing functions around

So I can do this:

(defparameter *some-function* ... ; returns lambda later

or this:

(defun some-function ...

With either, I can use funcall:

(funcall 'some-function ... or (funcall *some-function* ...

With the defun version I can also do this:

(some-function ...

I cannot do that with the defparameter function.

defparameter provides easier technique for re-assigning some-function to a different function (or anything else, including non-function data) later.

But other than these two points, what are other considerations of using one over another?

like image 612
johnbakers Avatar asked Oct 31 '13 14:10

johnbakers


1 Answers

This is an odd one to answer as we are somewhat comparing apples with oranges.

For those new to lisp who are looking at this, defparameter is for defining a dynamic variable whereas defun is for defining a function.

If you are worried about being able to programmatically reassign a function without using defun check out the following:

CL-USER> (defun jam () (print 'some-jam))
JAM

CL-USER> (jam)
SOME-JAM 

CL-USER> (setf (symbol-function 'jam) (lambda () (print 'some-ham)))
#<FUNCTION (LAMBDA ()) {1004C033DB}>

CL-USER> (jam)
SOME-HAM 

So defparameter doesn’t have an advantage when it comes to reassigning a function. Also if you want to redefine the function you could look into the compile command.

like image 147
Baggers Avatar answered Oct 29 '22 19:10

Baggers