Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Clojure language use so many parenthesis? [closed]

Tags:

syntax

clojure

It seems as if everything needs to be wrapped in parenthesis in clojure. Even a simple hello world!

(println "Hello world!")

What is the benefit of that syntax decision?

like image 948
Christoph Avatar asked Jul 09 '12 08:07

Christoph


People also ask

Why does Lisp use so many parentheses?

They mean that there needs to be a simple way of representing a tree structure within Lisp code. The purpose of the parentheses is to enable that representation. In particular, an open parenthesis, "(", means "step down a level of the tree". And a close parenthesis, ")", means "step back up a level of the tree".

Why is Clojure not a Lisp?

Clojure is a Lisp-1 and is not intended to be code-compatible with other dialects of Lisp, since it uses its own set of data structures incompatible with other Lisps. As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system.

What makes Clojure different?

Immutable data structures Clojure has features of an object-oriented language. So, it initially includes a set of immutable (unchangeable) structures and methods for working with them. They look like common JavaScript arrays, Hash Maps. But any operation can't change their values.

What is Clojure best used for?

Clojure is being used extensively for processing large volumes of data. It is very well suited to data mining/commercial-AI (ie: Runa) and large scale predictions (aka WeatherBill). Clojure's concurrency story really helps in these data heavy domains where parallel processing is simply the only answer.


1 Answers

The fundamental reason is that Clojure was designed as a homoiconic language, i.e. code is expressed in the core data structures of the language. All Lisps share this property, but few other languages do. As a result, the entire language design of Lisps is strongly influenced by this decision.

The choice was made early in the design of Lisp that lists would be used for function invocation in the form:

(function arg1 arg2 arg3) => some result

This has lots of advantages:

  • A function call is expressed as a single data object known as a "form" (in this case the form is a list, but it could also be a vector, a hashmap, a literal value etc.). You can pass it around, modify it and transform it as needed before execution.
  • It's easy to construct such function calls using standard list operations - e.g. (cons function-symbol list-of-args)
  • The syntax is very easy to parse - everything between two parentheses is a self-contained expression. This makes writing parsers easy, and also helps with tooling (see the incredible array of paren-handling commands in emacs for example)

The function name could have been put outside the parentheses:

function (arg1 arg2 arg3) => some result

But this would have many disadvantages:

  • You can no longer express a function call in a single form. You'd have to pass around two separate data objects, the function name and the argument list.
  • It would make the syntax more complex - you would need additional syntax rules to recognise this as a function call (e.g. extra delimiters like "," between the arguments)
  • It would be harder to do code generation. Lisp was designed from the start to allow effective code generation - on the other hand if you've ever tried generating C++ source code then you will know how hard it can be in a language not designed in that way.
  • It does not actually reduce the number of parentheses overall. In fact, due to being a very concise language, Clojure usually ends up with less parentheses than equivalent code in Java / C#
like image 157
mikera Avatar answered Oct 23 '22 11:10

mikera