Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is #' (sharp-quote) notation unnecessary in CLISP?

I'm learning Lisp from the book 'Practical Common Lisp'. At one point, I'm supposed to enter the following bit of code:

[1] (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

I suppose the idea here is of course that remove-if-not wants a function that can return either T or NIL when an argument is provided to it, and this function is then applied to all symbols in the list, returning a list containing only those symbols where it returned NIL.

However, if I now write the following code in CLISP:

[2] (remove-if-not 'evenp '(1 2 3 4 5 6 7 8 9 10)
(2 4 6 8 10)

It still works! So my question is, does it even matter whether I use sharp-quote notation, or is just using the quote sufficient? It now seems like the additional sharp is only there to let the programmer know that "Hey, this is a function, not just some random symbol!" - but if it has any other use, I'd love to know about it.

I use GNU CLISP 2.49 (2010-07-07, sheesh that's actually pretty old).

like image 223
Anonymous Avatar asked Jan 28 '14 15:01

Anonymous


People also ask

Why is the sky blue short answer?

The sky appears blue to the human eye as the short waves of blue light are scattered more than the other colours in the spectrum, making the blue light more visible.

What is the real colour of sky?

As far as wavelengths go, Earth's sky really is a bluish violet. But because of our eyes we see it as pale blue.

Why is Earth's sky blue in daytime?

The Short Answer: Gases and particles in Earth's atmosphere scatter sunlight in all directions. Blue light is scattered more than other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.


1 Answers

Sharp-quote and quote do not have the same behaviour in the general case:

(defun test () 'red)

(flet ((test () 'green))
  (list (funcall 'test)
        (funcall #'test))) => (red green)

Calling a quoted symbol will use the function value of the quoted symbol (ie, the result of symbol-function). Calling a sharp-quoted symbol will use the value established by the lexical binding, if any, of the symbol. In the admittedly common case that there is no lexical binding the behaviour will be the same. That's what you are seeing.

You should get into the habit of using sharp-quote. Ignoring function bindings is probably not what you want, and may be confusing to anybody trying to understand your code.

like image 200
gsg Avatar answered Sep 28 '22 08:09

gsg