To complement the other good answers with direct answers to your questions:
What does try using a symbol mean? p is definitely a symbol; false positive?
Read the error messages literally: (EVAL (ENVIRONMENT) P)
and (EVAL P)
(unevaluated) are not symbols, but lists. In Common Lisp, the car of a form is not evaluated.
What's up with eval? Doesn't the evaluation of p yield the procedure print?
eval
is never called by your code (see previous answer). Even if it were, the result would be the symbol-value
of the symbol print
, not the symbol-function
/fdefinition
.
I thought Lisp procedures were first class objects. Why is Attempt 1 not working like in Scheme?
This has nothing to do with functions (I think the Common Lisp standard does not use the term "procedures" as the Scheme standards do.) being first class objects. This works in Common Lisp:
(let ((p #'print))
(funcall p "hello world"))
Edit:
Answers to the extra questions:
I was wondering why (setf (symbol-function 'p) #'print)
won't work this way
(setf (symbol-function 'p) 'print)
.
It's not really true that "the sharp sign(#) is supposed to disambiguate between a function and a variable with the same name", as you write later. 'print
expands to (quote print)
, so it evaluates to the symbol print
instead of its value as a variable. #'print
expands to (function print)
, so it evaluates to the value of the function cell of the symbol print
instead. Whether print
currently has a value as a variable is completely irrelevant to what #'print
evaluates to.
Setting (symbol-function 'p)
to the symbol print
obviously won't make p
call the function print
because the symbol print
is not the same as the function bound to the symbol print
.
Also, why won't it work with defvar instead of setf like so:
(defvar (symbol-function 'p) #'print)
yet defvar and setf both assign values to a variable.
setf
assigns values to places. The term (symbol-function 'p)
denotes the place that is the function cell of the symbol p
.
defvar
defines new global variables. Its first argument needs to be a symbol that names the variable and can not be any kind of place.