Clojure-jvm停的lisp

Clojure--jvm下的lisp
作为当今最主流的运算平台JVM,把函数式编程语言引入JVM也是很多人尝试的方向,Clojure就是其中之一。Clojure是一个在JVM平台运行的动态函数式编程语言,其语法解决于LISP语言,在JVM平台运行的时候,会被编译为JVM的字节码进行运算。

Clojure保持了函数式语言的主要特点,例如immutable state,Full Lisp-style macro support,persistent data structures等等,并且还能够非常方便的调用Java类库的API,和Java类库进行良好的整合。

(new java.util.Date)
=> Wed Oct 17 20:01:38 CEST 2007

(. (new java.util.Date) (getTime))
=> 1192644138751 

(.. System out (println "This is cool!"))
This is cool!


Lisp风格的宏

(defmacro time [form]
  `(let [t0# (. System (currentTimeMillis))
         res# ~form
         t1# (. System (currentTimeMillis))]
    (.. System out (println (strcat "Execution took "
                                    (/ (- t1# t0#) 1000.0) " s")))
    res#))

Usage:
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))

(time (factorial 1000))
=> Execution took 0.012 s
     40…


Clojure的主页: http://clojure.org/