Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does # mean in LISP

For example, #'functionname, is it necessary?

like image 604
Thomson Avatar asked Feb 02 '11 11:02

Thomson


1 Answers

#'functionname in Common Lisp

Common Lisp and some other Lisp dialects have more than one namespace. Here the ones for functions and values are different. To get the function value of a name, we need to write:

(function functionname)

Since that is a bit long to write, there is a shorter notation:

#'functionname

To show the effect see this:

(let ((foo 42))
  (flet ((foo () 'bar))
    (list foo (function foo) #'foo (foo))))

Above defines a local variable FOO and a local function FOO. The list statement returns the value of FOO, then the function value of foo using the (function ...) notation, then the same using the short hand notation and then the value of actually calling the function FOO.

(function foo) and #'foo are the same concept, but written differently. Both refer to the local lexical function named FOO. FOO is introduced by the FLET form as a local function.

The Lisp REPL returns something like this:

(42 #<function FOO 4060008224> #<function FOO 4060008224> BAR)

Above shows that the first item is really the value of the variable FOO and the next two items are the function values, the function bound to FOO. The last item is the symbol BAR, as returned from the function call (FOO).

This separation of namespaces for normal values and function values is present in Common Lisp, Emacs Lisp and ISLisp. Other Lisp dialects like Scheme don't have that separation. In Scheme a name can only denote one value.

The # character.

The # character is used to introduce special syntax in s-expressions. Here are some examples:

#'functionname   ->  (function functionname)
#(1 2 3)         ->  the vector of the elements 1 2 3
#c(1 2)          ->  a complex number
#xFFFF           ->  a hex number
#b1111           ->  a binary number

and many more. # is a so-called dispatching macro character.

The ANSI Common Lisp HyperSpec describes the # character in Section 2.4.8 Sharpsign.

Common Lisp could have used a different syntax for vectors. Say [1 2 3]. It also could have used a different syntax for complex numbers. Something like {1 2}. But it does not do that. Why? The reason is because Common Lisp tries to be economical with character usage in the language and leaves characters like [, ], { and } to the user for his/her own syntax extensions. Often Lisp users develop embedded languages and to make that a bit easier, the Common Lisp standard tries to keep character usage down to a minimum and also provides the mechanism of macro characters and dispatch macro characters.

To keep character usage down, a single dispatch character # is used and the next character then determines what can be denoted. #b for binary numbers. #x for hex numbers. #c for complex numbers. #( for vectors. #' for function names. Plus many more.

Since Common Lisp is a programmable programming language, this character level syntax can be changed by the user. See the function SET-DISPATCH-MACRO-CHARACTER.

like image 80
Rainer Joswig Avatar answered Nov 11 '22 10:11

Rainer Joswig