4

In lisp, a symbol can be bound to both a value and a function at the same time. For example,

Symbol f bound to a function

(defun f(x)
    (* 2 x))

Symbol f bound to a value

(setq f 10)

So i write something like this:

(f f)

=> 20

What is the benefit of such a feature?

Drew
  • 29,895
  • 7
  • 74
  • 104
Varun Natarajan
  • 723
  • 1
  • 6
  • 6
  • 3
    The arguments and a link to Gabriel's Lisp-1/Lisp-2 paper are already discussed in [Separate Namespaces for Functions and Variables in Common Lisp versus Scheme](http://stackoverflow.com/questions/1020968/separate-namespaces-for-functions-and-variables-in-common-lisp-versus-scheme) – Pete Kirkham Nov 13 '10 at 10:31
  • @Pete: Thanks for the link. I searched for this topic before I posted my question but somehow I missed this one. – Varun Natarajan Nov 13 '10 at 10:45

2 Answers2

11

The symbol can have both a function and a value. The function can be retrieved with SYMBOL-FUNCTION and the value with SYMBOL-VALUE.

This is not the complete view. Common Lisp has (at least) two namespaces, one for functions and one for variables. Global symbols participate in this. But for local functions the symbols are not involved.

So what are the advantages:

  • no name clashes between identifiers for functions and variables.

    Scheme: (define (foo lst) (list lst))

    CL: (defun foo (list) (list list))

  • no runtime checks whether something is really a function

    Scheme: (define (foo) (bar))

    CL: (defun foo () (bar))

    In Scheme it is not clear what BAR is. It could be a number and that would lead to a runtime error when calling FOO.

    In CL BAR is either a function or undefined. It can never be anything else. It can for example never be a number. It is not possible to bind a function name to a number, thus this case never needs to be checked at runtime.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
-1

It's useful for everyday tasks, but the main reason is because of macros, you'll understand why once you study it.

drdo
  • 122
  • 3