Subject: Re: #Hash#
From: Erik Naggum <erik@naggum.net>
Date: Fri, 27 Apr 2001 01:50:02 GMT
Newsgroups: comp.lang.lisp
Message-ID: <3197325002699661@naggum.net>

* samwise gamgee <gamgee50@yahoo.com>
> Can anyone tell me the difference between these two lines of code:
> (MAPCAR '+ '(2 3 4))
> (MAPCAR #'+ '(2 3 4))
> i.e. what does the hash symbol do (if anything)?

  The hash is a reader macro character that, unlike ' and ( and ;, does not
  immediately cause a reader function to be called.  Instead, it reads
  another character which determines which reader function to call.  #'
  means quote as function object, ' means quote as object.  ( means start a
  list, #( means start a vector.  ; means start a comment, #; has no
  meaning.  :foobar is a keyword symbol, #:foobar is an uninterned symbol.
  #P introduces a pathname, #A an array, #X a hexadecimal number.  #\x is
  the character x, which may be the name of a character, not just a literal.

  The difference between #'+ and '+ is that #'+ is read as (function +),
  while 'x is read as (quote x).  Therefore the two lines of code are
  actually read as the Common Lisp code:

(mapcar (quote +) (quote (2 3 4)))
(mapcar (function +) (quote (2 3 4)))

  (quote +) returns the argument _unevaluated_, or quoted.

  (function +) does not evaluate the argument, but looks it up in the
  lexical environment for a functional definition.  There are some subtle
  differences between what mapcar will do with a symbol and what the
  function special operator will do with its argument.  E.g.,

(defun foo (x) (expt x 2))

(flet ((foo (x) (expt 2 x)))
  (values (mapcar 'foo '(1 2 3 4))
	  (mapcar #'foo '(1 2 3 4))))
=> (1 4 9 16)
=> (2 4 8 16)

  If you define a function named (setf foo), you cannot refer to it with
  '(setf foo), but must use #'(setf foo), and you cannot access the global
  version if you shadow it with flet using notation -- you need a separate
  variable to hold the shadowed global value.

  It may be pedagogically sound to write things out using explicit forms,
  like quote and function, until you understand how they work, and then you
  will appreciate their abbreviated syntactic expression.

#:Erik
-- 
  I found no peace in solitude.
  I found no chaos in catastrophe.
			-- :wumpscut: