Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in clojure language what <'a> really is

actually i am trying to perfectly understand clojure and particularly symbols

(def a 1)
(type a)
;;=>java.lang.Long
(type 'a)
;;=>clojure.lang.Symbol

I know that type is a function so its arguments get evaluated first so i perfectly understand why the code above work this way .In the flowing code i decided to delay the evaluation using macro

 (defmacro m-type [x] (type x))
 (m-type a)
 ;;==>clojure.lang.Symbol

and i am fine with that but what i fail to uderstand is this:

 (m-type 'a)
 ;;=>clojure.lang.Cons

why the type of 'a is a cons

like image 635
user3228423 Avatar asked Jan 23 '14 17:01

user3228423


People also ask

What is the Clojure language?

Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures.

What is Clojure based on?

Clojure runs on the Java platform and as a result, integrates with Java and fully supports calling Java code from Clojure, and Clojure code can be called from Java, too. The community uses tools like Leiningen for project automation, providing support for Maven integration.

Why is Clojure a Lisp?

Clojure is a member of the Lisp family of languages. Many of the features of Lisp have made it into other languages, but Lisp's approach to code-as-data and its macro system still set it apart. Clojure extends the code-as-data system beyond parenthesized lists (s-expressions) to vectors and maps.

Where is Clojure language used?

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 character ' is interpreted by the clojure reader as a reader-macro which expands to a list containing the symbol quote followed by whatever follows the ', so in your call to (m-type 'a) the 'a is expanding to:

user> (macroexpand-1 ''a)
(quote a) 

then calling type on the list (quote a) which is a Cons.

This may be a bit more clear if we make the m-type macro print the arguments as it sees them while it is evaluating:

user> (defmacro m-type [x] (println "x is " x) (type x))
#'user/m-type
user> (m-type 'a)
x is  (quote a)
clojure.lang.Cons  
like image 179
Arthur Ulfeldt Avatar answered Nov 03 '22 17:11

Arthur Ulfeldt