第一个clojure 的hello world程序

来源:互联网 发布:中信银行淘宝卡 编辑:程序博客网 时间:2024/04/30 04:45

clojure是一种可以在java虚拟机上运行的函数式编程语言。storm就是用clojure和java编写的。
环境:
ubuntu 12.04
1.apt-get install lein
安装lein,leinigen是clojure的项目管理工具,类似于maven是java的项目管理工具一样。
2.apt-get install clojure
安装clojure的运行环境
3.lein new hello
创建一个hello的clojure项目
4.修改代码:
hello/src/hello.core
(ns hello.core)
(defn -main
[& args]
(println “Hello, World!”))
修改project.clj
增加
:main hello.core
指定main函数
lein run
或者
lein run hello.core
显示
Hello, World!
5.也可以通过repl的方式执行
lein repl
user=> (require ‘hello.core)
nil
user=> (hello.core/-main)
Hello, World!
nil
6.也可以不依赖lein,直接用clojure执行。
编写PigLatin.clj
; This is Clojure code.
; When a set is used as a function, it returns a boolean
; that indicates whether the argument is in the set.
(def vowel? (set “aeiou”))

(defn pig-latin [word] ; defines a function
; word is expected to be a string
; which can be treated like a sequence of characters.
(let [first-letter (first word)] ; assigns a local binding
(if (vowel? first-letter)
(str word “ay”) ; then part of if
(str (subs word 1) first-letter “ay”)))) ; else part of if

(println (pig-latin “red”))
(println (pig-latin “orange”))

执行clojure PigLatin.clj
返回
edray
orangeay

7.要使vim能够彩色地显示clojure代码,按下面的教程安装相关的插件就可以了。

https://github.com/daveray/vimclojure-easy

0 0