Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of a symbol that binds to a value and function at the same time?

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?

like image 799
Varun Natarajan Avatar asked Nov 13 '10 09:11

Varun Natarajan


People also ask

Why is it important to bind a function to its arguments C++?

Bind function with the help of placeholders helps to manipulate the position and number of values to be used by the function and modifies the function according to the desired output.

What is the use of symbol data type in JavaScript?

Symbols are often used to add unique property keys to an object that won't collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.

Why do we use symbol?

Symbols facilitate understanding of the world in which we live, thus serving as the grounds upon which we make judgments. In this way, people use symbols not only to make sense of the world around them, but also to identify and cooperate in society through constitutive rhetoric.

Is defined as symbols that represent properties of objects events and their environment?

Data are symbols that represent properties of objects, events and their environment.


1 Answers

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.

like image 177
Rainer Joswig Avatar answered Sep 21 '22 21:09

Rainer Joswig