What is the point of using def and defn instead of just defining?

In the diagram, we just defined for the whole definition why Clojure and Lisp use different keywords for different ads?

+6
lisp clojure scheme
source share
3 answers

defn , defmacro , defstruct , and these are just macros to determine their corresponding structure, which makes it unnecessary to write boilerplate code. For example, defn makes it easy to write functions, since you no longer have to worry about entering a lambda term (which you can do in the Scheme by simply copying the arguments immediately after the name is defined):

Scheme:

 (define hello (lambda (name) (display (string-append "hello, " name)))) (define (hello name) (display (string-append "hello, " name))) 

Clojure:

 (def hello (fn [name] (println (str "hello, " name)))) (defn hello [name] (println (str "hello, " name))) 

You can look at defn as a mnemonic device for def + fn . Such labels make less coding and make the code easier to read as a whole. I teach someone how to program Scheme at the moment, and I force them to use long forms so that they always keep in mind what this language does (and they will appreciate the work saving macro when they finally start using them).

The define or def forms name the following expression. The lambda or fn form gives the name a set of bindings when used in a define or def expression. That things are starting to get more complicated, and using macros keeps you healthy and protects you from typing boredom.

+15
source share

They define different types of objects (functions, special variables, macros, structures, classes, general functions, methods, packages). In Common Lisp, they are also in different namespaces. Different forms of definition also have different syntax (Scheme define combines the definition of variables and functions, but can it define a class?)

+2
source share

def in Clojure is a special form, and it's nice to keep special forms as simple and primitive as possible. defn is a macro that adds sugar for easy docker and metadata setup, and (as others have said) eliminates the need for extraneous pairs. defn also automatically sets some metadata in your function itself (showing arbiters, etc.), which does not make sense for general def .

Functions are by far the most common that you intend to define for the top level in Clojure. Toplevel's mutable variables are discouraged. Therefore, it makes sense to have a simple and quick way to define top-level functions. Using def for all will result in a lot of patterns. If there wasn’t defn , we would all have invented it ourselves so as not to annoy death.

+1
source share

All Articles