From: Scream
Subject: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <1146447520.056326.115570@v46g2000cwv.googlegroups.com>
Hi,

I was reading an old book about Common Lisp [1]. It tried to explain
#'xxx - aka (function xxx) - but I didn't get it. [2]

Why does the language distinguish between the name of the object and
the object itself? I realise the two are distinct but I would assume
that the language would automagically substitute the object for the
name as necessary.

What is the difference between x and y in the following?

  ( defun x ... )
  ( setf y ( function x ))

Further, if there's the necessity to do this for functions, is there a
similar necessity for numbers? Is there such a construct as (number
ten)? Not so far as I've seen so why are functions different from other
types?

(My background is C++, just in case that may be causing me confusion)

Thanks,
Brian.

[1] "Lisp: 3rd Edition"
     by Patrick Henry Winston
     &  Berthold Klaus Paul Horn
     Addison Wesley 1989

[2] From Page 52:
"Of course, you may wonder why things have to be so fancy. Why not have
LISP expect a procedure name following the :TEST keyword, so that the
#' would be unnecessary? After all, LISP expects the first element in
forms to be procedure names and must produce procedure objects from
those names. The reason is that we want to allow a keyword argument to
be a variable whose value is a procedure object, rather that a
procedure name. Suppose, for example, that we bind PREDICATE to a
procedure object:

  ( setf predicate #'equal )

Now we can use PREDICATE's value, a procedure object, in a member form:

  ( member 'apple fruits :test predicate )

From: Tel A.
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <1146450083.498597.294430@e56g2000cwe.googlegroups.com>
(A disclaimer: I'm no expert, but I think I get this fairly well, so
I'll take a stab at it.)

Lisp does, for the most part, automagically convert names to the
objects they represent, provided it can figure out which object you
need. In Common Lisp (but not Scheme) there are two namespaces -- the
function/macro namespace and the variable namespace -- which are
considered depending upon the context in which a symbol is encountered.

CL syntax says the first symbol in a list is to be interpreted as a
function, so that symbol is converted to an object according to the
function/macro namespace. In all other positions, CL looks the symbol
up in the variable namespace.

The sharp-quote or FUNCTION special operator is necessary to force lisp
to search for the passed symbol in the function namespace in the event
you need to have a function represented in a position other than the
first cell of a list.

As to the difference between x and y after  (defun x ...) and (setf y
(function x))...
In the functional namespace, they are the same

(eq #'x y)
=> T

But if you simply try (eq x y) lisp will return an error because you
are tying to reference x in the variable namespace where it has no
value.

The practical value of an operation like (setf y (function x)) is that
you can call x through y using FUNCALL/APPLY. This is used in every
function that takes another function as a parameter.

The dual namespace is a bit different and actually subject to huge
debate between supporters of different lisps. Common Lisp with its two
namespaces is considered a Lisp-2 where Scheme, which has only one, is
a Lisp-1.

Hope that answers the question adequately.
From: Scream
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <1146450410.685623.101110@j33g2000cwa.googlegroups.com>
Tel A. wrote:
> Lisp does, for the most part, automagically convert names to the
> objects they represent, provided it can figure out which object you
> need. In Common Lisp (but not Scheme) there are two namespaces -- the
> function/macro namespace and the variable namespace -- which are
> considered depending upon the context in which a symbol is encountered.
See? That was easy. Why couldn't the book have said that?

> Hope that answers the question adequately.
Yes, thanks.
From: Tel A.
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <1146450884.549740.203550@j73g2000cwa.googlegroups.com>
Though clearly not very succinctly! Eli said all that in two sentences.
From: Eli Gottlieb
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <C7e5g.5075$Gg.636@twister.nyroc.rr.com>
Scream wrote:
> Hi,
> 
> I was reading an old book about Common Lisp [1]. It tried to explain
> #'xxx - aka (function xxx) - but I didn't get it. [2]
> 
> Why does the language distinguish between the name of the object and
> the object itself? I realise the two are distinct but I would assume
> that the language would automagically substitute the object for the
> name as necessary.
> 
> What is the difference between x and y in the following?
> 
>   ( defun x ... )
>   ( setf y ( function x ))
> 
> Further, if there's the necessity to do this for functions, is there a
> similar necessity for numbers? Is there such a construct as (number
> ten)? Not so far as I've seen so why are functions different from other
> types?
> 
> (My background is C++, just in case that may be causing me confusion)
> 
> Thanks,
> Brian.
> 
> [1] "Lisp: 3rd Edition"
>      by Patrick Henry Winston
>      &  Berthold Klaus Paul Horn
>      Addison Wesley 1989
> 
> [2] From Page 52:
> "Of course, you may wonder why things have to be so fancy. Why not have
> LISP expect a procedure name following the :TEST keyword, so that the
> #' would be unnecessary? After all, LISP expects the first element in
> forms to be procedure names and must produce procedure objects from
> those names. The reason is that we want to allow a keyword argument to
> be a variable whose value is a procedure object, rather that a
> procedure name. Suppose, for example, that we bind PREDICATE to a
> procedure object:
> 
>   ( setf predicate #'equal )
> 
> Now we can use PREDICATE's value, a procedure object, in a member form:
> 
>   ( member 'apple fruits :test predicate )
> 
You need them because CL has separate namespaces for functions and 
values.  Hence, the function operator (and its shorthand #') are 
required to look for the given name in the /function/ namespace.

-- 
The science of economics is the cleverest proof of free will yet 
constructed.
From: David Sletten
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <87h5g.3787$yM.1318@tornado.socal.rr.com>
Scream wrote:

> Hi,
> 
> I was reading an old book about Common Lisp [1]. It tried to explain
> #'xxx - aka (function xxx) - but I didn't get it. [2]
> 
> Why does the language distinguish between the name of the object and
> the object itself? I realise the two are distinct but I would assume
> that the language would automagically substitute the object for the
> name as necessary.
> 
> What is the difference between x and y in the following?
> 
>   ( defun x ... )
>   ( setf y ( function x ))
> 
> Further, if there's the necessity to do this for functions, is there a
> similar necessity for numbers? Is there such a construct as (number
> ten)? Not so far as I've seen so why are functions different from other
> types?
> 
> (My background is C++, just in case that may be causing me confusion)

The fundamental question here is what do the symbols in our programs 
mean? (I am using "symbol" in the general sense of a related sequence of 
characters, broader than the Lisp idea of a symbol. Maybe "token" would 
be a better word). Certain "symbols" represent themselves while others 
may represent data distinct from themselves. In Common Lisp the first 
group are known as self-evaluating objects:
http://www.lispworks.com/documentation/HyperSpec/Body/03_abac.htm
Obvious examples are the numeral 3, which represents the integer three, 
or the string literal "pung", which represents that sequence of 
characters delimited by quotation marks.

With the other group we have to establish what they represent. This is 
called binding. Common Lisp is known as a Lisp-2 Lisp, which means that 
it has 2 distinct namespaces--one for function bindings and one for 
variable bindings. Another prominent dialect called Scheme has a single 
namespace, so it is a Lisp-1 Lisp.

Since a name can "mean" two different things, Common Lisp decides which 
binding to refer to based on context. For example if you evaluate a CONS 
which has a symbol as its first element, then that symbol's function 
binding (assuming it has one--it could name a macro or special operator 
instead) is used:
(reverse '(a b c d))
http://www.lispworks.com/documentation/HyperSpec/Body/03_abab.htm

The function associated with REVERSE is applied to the list.

If a symbol itself is evaluated as a form, then the symbol's variable 
binding is used:
(+ 2 x)
http://www.lispworks.com/documentation/HyperSpec/Body/03_abaa.htm

The value of the variable named X is used as an arg to +.

However, there may be cases when you want to use the function binding of 
a symbol as data, perhaps as an argument to some other function. This is 
one of the reasons for FUNCTION (or #' which we usually call "sharp 
quote"). For example, the SORT function takes an argument that tells it 
_how_ to sort. We may want to sort a list of numbers in ascending or 
descending order:
[9]> (defvar *numbers* (list 9 8 1 2 3 6))
*NUMBERS*
[10]> (sort *numbers* #'>)
(9 8 6 3 2 1)
[11]> (sort *numbers* #'<)
(1 2 3 6 8 9)

Had we not used #' above, then the variable binding of > and < would 
have been used, and presumably these symbols don't actually name variables.

Here's a simple example of a name taking on both variable and function 
bindings and the distinction:
(let ((f 2))
   (flet ((f (obj) (type-of obj)))
     (print (f f))
     (print (f #'f))
     (print (funcall #'f f))))

In the first call to PRINT we call the function F with the value of the 
variable F as arg. The second and third calls to PRINT illustrate using 
the function value of F as an argument to the function itself and then 
to FUNCALL.

As you can see there is no chance of confusion as long as you are paying 
attention. If you see something such as (list list), then it is clear 
that the first "list" refers to a function and the second refers to a 
variable. Likewise, (list #'list) unambiguously means something different.

Of course a variable can contain any kind of value--including a function:
(flet ((f (x) (* x 2)))
   (let ((g #'f))
     (print (f 3))
     (print (funcall g 3))))

Here the value of G as a variable is the value of F as a function. 
Notice that we do _not_ use #' with G in the call to FUNCALL. We need to 
pass a function to FUNCALL, and that's exactly what the _variable_ G 
refers to.

I don't know about C++, but a similar situation exists in Java. The same 
identifier may be used to name both a method and a variable, and Java 
determines the usage based on context:
f = f(11);

However, Java does not provide the analog of FUNCTION to allow you to 
specify the function binding of an identifier.

Aloha,
David Sletten
From: einjun
Subject: Re: Why do we need #'xxx (hash quote) and (FUNCTION xxx)?
Date: 
Message-ID: <1146495144.368571.145910@u72g2000cwu.googlegroups.com>
You need to understand the concept of namespaces as they relate to
Lisp. Winston and Horn is a fine book. IMHO, it doesn't touch on some
fundamental concepts though.  Please consider the following books
available online:
1) Common Lisp: A Gentle Introduction to Symbolic Computation
http://www.cs.cmu.edu/~dst/LispBook/

2) Practical Common Lisp:
http://www.gigamonkeys.com/book/

I suggested the above mentioned books because they are available
immediately online. If you're considering paperbacks, plenty good Lisp
books are available. Please do a search on the newsgroup or consult a
lisp website.


> procedure name. Suppose, for example, that we bind PREDICATE to a
> procedure object:
>
>   ( setf predicate #'equal )
>
> Now we can use PREDICATE's value, a procedure object, in a member form:
> 
>   ( member 'apple fruits :test predicate )