Do any lisps have a s-expression as their head, e.g. ((f 2) 3 4)? If not, why?
Asked Answered
T

3

7

Do any lisps support nested s-expression on their head? For example

((f 2) 3 4)

for which (f 2) presumably evaluates to a function/macro to apply on 3 4.

Is it possible to have a lisp supporting such a thing? Or are there technical limitations that prohibit this/make it impractical?

Treacherous answered 16/10, 2012 at 3:4 Comment(0)
W
12

In those Lisps, which have single namespace for variables and functions, your expression is valid. These are called Lisp-1. Scheme and Clojure are examples of such Lisps.

In those Lisps, which have separate namespaces for variables and functions, your expression would be (funcall (f 2) 3 4). These are called Lisp-2. Common Lisp and Emacs Lisp are examples of such Lisps.

In Lisp-2 every symbol has a value slot and a function slot. To call a function, that is stored in a value slot you need to use funcall keyword.

See more on this issue: http://www.dreamsongs.com/Separation.html

Edit: Thanks to Rainer Joswig i corrected the answer.

Wildawildcat answered 16/10, 2012 at 3:9 Comment(2)
the example now is okay. The explanation not really. The symbol slot is only half of the problem. 'Lisp-2' has two namespaces. Values are not stored in slots. Values are the result of evaluating expressions. (let ((a (lambda () 'hi))) (funcall a)) In this example in Common Lisp there is no 'symbol value' involved. It is a lookup of a lexical variable. It is not an access to a value of a symbol. The symbol is only an artifact of the source code, but not of the actual execution.Decorator
I think you should reword your second paragraph slightly. Perhaps starting with "In Lisp-2s, on the other hand, there are separate namespaces..." I got confused at first because I thought you were still talking about Lisp-1's in your second paragraph. Nevertheless, accepted.Treacherous
D
5

For example in Common Lisp above is not valid. The syntax of Common Lisp does not generally allow lists as the head of a function call. You have to use FUNCALL to call a returned function value.

(funcall (f 2) 3 4)

In some other Lisp dialects it is allowed. Scheme is such a Lisp dialect. Scheme also evaluates the head of a function call expression.

Decorator answered 16/10, 2012 at 6:38 Comment(0)
R
1

Lisp-1 lisps, such as Scheme, usually have all expressions of a function form evaluated, even the function itself.

Lisp-2 lisps, such as Common Lisp, usually have different behaviour for the function and for the arguments. Whereas the arguments are evaluated, the function is looked up. The common way to invoke an evaluated function is to use funcall or apply.

(funcall (f 2) 3 4)

In Common Lisp, you can use a lambda form, if you insist on evaluating something to a function in the operator:

((lambda (&rest args) (apply (f 2) args)) 3 4)
Raddatz answered 17/10, 2012 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.