From ... Path: newsfeed.google.com!newsfeed.stanford.edu!news.ems.psu.edu!news.cis.ohio-state.edu!news.maxwell.syr.edu!npeer.kpnqwest.net!nreader2.kpnqwest.net.POSTED!not-for-mail Newsgroups: comp.lang.lisp Subject: Re: #Hash# References: <3AE85B97.208F965E@yahoo.com> Mail-Copies-To: never From: Erik Naggum Message-ID: <3197325002699661@naggum.net> Organization: Naggum Software, Oslo, Norway Lines: 51 User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 27 Apr 2001 01:50:02 GMT X-Complaints-To: newsmaster@Norway.EU.net X-Trace: nreader2.kpnqwest.net 988336202 195.0.192.66 (Fri, 27 Apr 2001 03:50:02 MET DST) NNTP-Posting-Date: Fri, 27 Apr 2001 03:50:02 MET DST Xref: newsfeed.google.com comp.lang.lisp:9372 * samwise gamgee > 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: