blackened
blackened

Reputation: 903

Clojure: Definitions of basic terms

In Clojure context, some define the term form as “any valid code,” and some as “any valid code that returns a value.” So both the numeral 1729 and the string Hello! is a form. Likewise (fn is not a form. Is an undefined symbol, say my-val, a form?

What is the difference between an expression and a form?

What is the difference between an expression and a function?

Upvotes: 1

Views: 136

Answers (2)

Alex Miller
Alex Miller

Reputation: 70211

There are some good answers to this question at Are Lisp forms and Lisp expressions same thing?

The key thing to think about is that there are different points in the lifecycle. We start with text "(+ 1 2)" which is then read into Clojure data (a list containing a symbol and two numbers). Often in Lisps "expression" is used to mean the former and "form" is used to mean the latter. In practice, I do not find that people are at all consistent with this usage and often use both terms for both things.

If you take "form" to mean "something which can be evaluated", then 1729 or "Hello!" or the symbol my-val are all forms. When my-val is evaluated it is resolved in the current namespace, perhaps to a function instance, which is invokable. Functions are really important only at evaluation time, when they can be invoked.

Another interesting aspect are macros, which allow you to create new syntax. Note that macro expansion happens after reading though, which means that while you can create new syntax, it still must follow some basic expectations that are encoded into the reader (namely that invocations follow the pattern (<invokable> <args...>)). Note that macros work on read but unevaluated forms (data, not text) and must produce new forms.

Upvotes: 2

fl00r
fl00r

Reputation: 83680

What is the difference between an expression and a form?

In my opinion form in a context of Clojure is something a compiler deals with. Some forms are valid expressions while others are "special" forms (i.e. macros).

What is the difference between an expression and a function?

Any function is an expression.

Is an undefined symbol, say my-val, a form?

I would say it is a valid expression (hence form) which yields to a compile time exception.

Likewise (fn) is not a form

It seems like you are referring to some source, where this was declared, could you provide a link?

Upvotes: 0

Related Questions