0

In Scheme, you define functions like

(define f (lambda (x) ...))

In particular, you can do something like this

(define f (g))

where g is some function returning a function. Is it possible to do the same in Common Lisp, i.e. to associate a function symbol with a given anonymous function?

1 Answer 1

1

Nevermind, I just found the answer in Paul Graham's book ANSI Common Lisp (after looking the second time; p. 99):

(setf (symbol-function 'f) (lambda (x) (* x x)))

achieves (for most intents and purposes) the same as

(defun f (x) (* x x))
Sign up to request clarification or add additional context in comments.

3 Comments

defun does much more than that. (macroexpand '(defun f (x) (* x x)))
That's why I qualified the statement with "for most intents and purposes". This is mentioned in Graham's Book as well.
"For most intents and purposes" is quite subjective. If you're compiling files, defun informs the compiler about the function to avoid reference warnings in the same file, even if the function is declared after the references. Most implementations annotate where they found definitions such as defuns, so that an IDE or Emacs+Slime can jump to them. You don't get any of that with setf of symbol-function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.