5

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?

johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Depending on what exactly you're trying to do, fundamentally, you _may_ also want to consider `flet` and/or `labels`, which I find a nice description of [here](http://www.gigamonkeys.com/book/the-special-operators.html#manipulating-the-lexical-environment). – lindes Oct 31 '13 at 23:47

1 Answers1

9

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.

Baggers
  • 3,183
  • 20
  • 31
  • 5
    You can also use `(setf (fdefinition 'jam) ...)` here, and [`fdefinition`](http://www.lispworks.com/documentation/HyperSpec/Body/f_fdefin.htm) will work on _all_ function names (including `(setf ...)` functions, not just symbols. – Joshua Taylor Oct 31 '13 at 14:54
  • @JoshuaTaylor: Brilliant, I didn’t know about that. Cheers (also good point about let) – Baggers Oct 31 '13 at 14:54
  • 2
    There's still an advantage with `defparameter` that `let` will bind a new value and _restore_ the old value automatically for you, without you having to store it explicitly. E.g., compare the two snippets in [this paste](http://pastebin.com/8sLLnEPJ). – Joshua Taylor Oct 31 '13 at 14:56
  • For those looking at *that paste*, consider using `unwind-protect` to restore the fdefinition. – Lars Brinkhoff Oct 31 '13 at 15:05
  • 1
    Note that "A call within a file to a named function that is defined in the same file refers to that function, unless that function has been declared notinline. The consequences are unspecified if functions are redefined individually at run time or multiply defined in the same file." http://clhs.lisp.se/Body/03_bbc.htm – Lars Brinkhoff Oct 31 '13 at 15:10
  • 1
    Or just use `defun` again: `(defun jam () (print 'some-ham))`. ;-) `defun` is not like `defvar` in not updating. – Drew Oct 31 '13 at 15:36
  • This question shows another important distinction between defparameter and defun: http://stackoverflow.com/questions/19919918/ – johnbakers Nov 12 '13 at 03:25