开发者

difference between Composite Form & Special Form

开发者 https://www.devze.com 2023-03-08 16:07 出处:网络
In page 19 of Pratical Clojure by Luke VanderHart & Stuart Sierra, Chapter 2, there is a paragraph,

In page 19 of Pratical Clojure by Luke VanderHart & Stuart Sierra, Chapter 2, there is a paragraph,

  • Special forms definition : Special forms are a particular type of composite form. For most purposes, they are used very similarly to a function call. The difference is that the first form of a special form is not a function defined somewhere, but a special system form that’s built into Clojure.

1st question, Is the form word is a typo here? Isn't the word will be item? If not, is def a form? That means, this code contains 3 forms. :-s

Cause later on, when the write was explaining an example, he used item.

(def message "Hello, World!")
  • Analyzing the first form, (def message "Hello, World!") , you see first that it is enclosed in parenthesis. Therefore, it is a list, and will be evaluated as a function application or a special form. There are three items in the list: def, message and "Hello, World!". The first item on the list, def, will be the function or special form that is called. In this case, it's a special form. But like a function, it takes two parameters—the var to define, and the value to which to bind it. Evaluating this form creates a var which establishes a binding of the value "Hello, World!" to the symbol message.

*2nd question, Is a composite form is a special form only when, the first item of a list is something that is only predefined withing Clojure? If yes, then is it possible to create custom special form? Is then the custom composite form is gonna be called as a special form? Cause, according to the definition, my custom is not something built in clojure, it is defined somewhere else.

EDIT: I guess the answer to my first question is no, its not a typo. def IS a form.

cause later on the write said,

  • The second form (println message) is also a list and this time it’s a normal function application. It has two component forms—each of them is a symbol

That means, (println message) > the whole thing is a form, and every thing else(function arguments) that it holds is also a form.

But, at the beginning the writer said, there are four basic varieties of forms.

  1. Literal,
  2. Symbol,
  3. Composite form and
  4. Special form.

According to the definition of Symbols from the book, its not a symbol.

  • Symbol Definition Symbols are forms which resolve to a value. They may be thought of as roughly similar to variables, although this is not technically accurate since they are not actually variable in the same way variables in most languages are. In Clojure, symbols are used to identify function arguments, and globally or locally defined values. Symbols and their resolution are discussed in more detail in the following sections. Cause, writing def and pressing return key produces the following error message,

In which categ开发者_如何学JAVAory, def falls into?


def is a special form. To see why, think about how you would evaluate a def expression if it were a function. When you type in an expression like (square (* x y)), you could evaluate the subexpression (* x y) first, and then apply square to whatever that is. This is not what happens with def, and for good reason, which you can see if you fire up a REPL:

> (def a 1)

#'user/a

> (def a 2)

...

What should happen next? If def were a normal function, the REPL would evaluate a, and then try to apply def to the arguments 1 and 2: that is, it would try to redefine 1. But, what you probably expect to do is to define the symbol "a" to evaluate to 2 (clobbering the old symbol in the process). To get the desired behavior out of def and a handful of other built-in procedures that would be broken if evaluated as functions, these special forms are either compiled into the interpreter or implemented as macros.


I wrote that chapter. Definitely could be written better, but I'll try to clear it up:

To answer your first question: Yes, form is the correct word. A single symbol (like def) is still a form, since a form just anything that can be eval'd. I do say 'item' sometimes, but that's just a more generic word I used to point out "things" in a list. Technically, if it's written in Clojure code and can be eval'd, it's a form.

For your second question, no, a composite form is anything built out of other forms. For example, if I define this:

(defn sq [x] (* x x))

And then call it,

(sq 3) => 9

the expression (sq 3) is a composite form comprised of two other forms, the first of which is the symbol sq which will eval to your function, the second is the literal 3.

The term "composite form" isn't a formal definition or anything, it's just the easiest way I could think of to express the fact that forms can be built out of other forms. Wherever I might want to put 3 it is valid syntax to put x and wherever I can put x I can put (f x) - because they're all just forms.

To answer your final question, def is a symbol that has special meaning to the compiler, so in the terminology I used in the book, it's a special form.


1st question:

Yes - I think "first item of a special form" would be more clear in this circumstance. But since a symbol counts as a form in its own right, calling it the "first form of a special form" is also technically correct.

Be aware that the first item in a special is itself sometimes referred to as a special form, as in "def is a special form". I think there's some confusion around the terminology here, but as long as you understand the concept of a special form then it is usually clear what is being meant. In general, you'll be correct if you think of "special form" as referring to the whole form.

The question on the definition of a Lisp form is quite helpful here.

2nd question

Yes - special forms only exist where the first symbol is one of the special form symbols predefined within Clojure. See http://clojure.org/special_forms for a useful list.

You can't currently define your own special forms - although it's possible to achieve similar results with macros or functions in for all practical urposes so it's never really a problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号