It is absolutely possible! You are almost there. You just came across Common Lisp with double namespaces, which many can get used to. Hopefully I can say something to make Common Lisp two namespaces a little less confusing.
Your code is almost correct. You wrote:
(defun foo (fn seq) (mapcar
But what is this trying to do? Well, # 'is a shorthand. I will expand it for you.
(defun foo (fn seq) (mapcar (function fn) seq))
So, the symbol # is an abbreviation (function symbol). In Common Lisp - as you know, characters can be bound to a function and variable; these are the two namespaces that Lispers talk about so much: the function namespace and the variable namespace.
Now, what does the special form of the function do, we get the function associated with the symbol, or, if you want, the value that the symbol has in the function namespace.
Now, on REPL, what you wrote will obviously be what you want.
(mapcar
Match the function of the car with a sequence of lists. And the car symbol does not have a variable binding, it is only a function binding, so you need to use the function (function ...) (or its shorthand, # ') to get the actual function.
Your foo function does not work, because the function you pass as an argument is bound to the character as a variable. Try the following:
(let ((fn
You might have expected a list of all the square numbers of these numbers, but that didn't work. This is because you used to bind the square root function to fn as a variable. Now try this code:
(let ((fn
Amazing! This binds the square root function to the symbol fn as a variable.
So let go review your foo function:
(defun foo (fn seq) (mapcar fn seq))
This will work because fn is a variable. Try testing it to make sure:
;; This will not work (foo sqrt '(4 9 16 25)) ;; This will work (foo #'sqrt '(4 9 16 25))
The first did not work because the square root function is associated with sqrt in the function namespace. So, in the second, we took the function from the symbol and passed it to foo, which bound it to the symbol fn as a variable.
Okay, so if you want to bind a function to a character in the function namespace? Well, for starters, defun does this, forever. If you want it to be temporary, like a let binding, use flet. Flat, in my opinion, is stupid, because it does not work exactly as it does. But, I will give an example, just so you can see.
(flet ((fn (x) (sqrt x))) (mapcar fn '(4 9 16 25)))
will not work because flet did not bind a function to a symbol in the variable namespace, but in the function namespace.
(flet ((fn (x) (sqrt x))) (mapcar
This will do what you expect, because flet has associated this function with the symbol fn in the function namespace. And just to control the idea of a function namespace:
(flet ((fn (x) (sqrt x))) (fn 16))
Will be back 4.