首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Clojure-jvm上的lisp

2012-11-12 
Clojure--jvm下的lisp作为当今最主流的运算平台JVM,把函数式编程语言引入JVM也是很多人尝试的方向,Clojure

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类库进行良好的整合。

[code="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!
[/code]

Lisp风格的宏

[code="java"]
(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…
[/code]

Clojure的主页: [url]http://clojure.org/[/url]

热点排行