-
Notifications
You must be signed in to change notification settings - Fork 77
2 Minute Intro
The purpose is to provide a quick 2 minute intro to Clojure that is embeddable into the main ClojureDocs page.
The 2 min intro should walk the user through bootstrapping a repl (lein, cljr, whatever) and doing something cool with it.
- Don't care about setting up a dev environment here.
- Bonus points for something unique to clojure.
- Maybe something like try-clojure.org?
- (str (interpose ...)) example?
- Web scraping example?
I'm thinking something like this:
First we're going to bootstrap our clojure environment.
sh$ gem install cake
sh$ cake repl
Don't have a windows env handy to test this, but I'm assuming the setup is similar http://github.com/ninjudd/cake/wiki/Cake-on-Windows
This will open a repl window like so (repl window screenshot). Enter (println "wakka wakka!")
and hit enter, the output should look something like:
user=> (println "wakka wakka!")
wakka wakka!
nil
Congratulations! You've just written and run your first Clojure program. There are a couple of things to note here:
- Method / function calls go inside parenthesis (this may look strange at first, but trust me, you'll get used to it quickly). Whereas in other languages this would look like
System.out.println("text to print")
orputs("yo")
, in Clojure you just move that first parenthesis to the begining like so:(System.out.println "text to print")
/(puts "yo")
. - The
user=>
text indicates you're in a (REPL)[http://en.wikipedia.org/wiki/Read-eval-print_loop], which is an interactive portal into your application. With Clojure you're not handcuffed by the Edit-Compile-Run cycle. - The last line (
nil
) was the return value of the function (println
) you called. Every function in Clojure returns a value, and in this case it happened to be the thing that Clojure uses to represent nothing,nil
.
Now lets try something a little more involved:
;; Square brackets '[ ]' are used to define lists of things.
user=> (def animals ["marmott" "aardvark" "hummingbird"])
#'user/animals
user=> (println animals)
[marmott aardvark hummingbird]
nil
user=> (println (sort animals))
(aardvark hummingbird marmott)
nil
--OR--
;; Square brackets '[ ]' are used to define lists of things.
user=> (def my-numbers [0 1 2 3])
#'user/my-numbers
user=> (map inc my-numbers)
(1 2 3 4)
user=> (reduce + (map inc my-numbers))
10
And if you're the adventurous type:
user=> (use 'clojure.java.browse)
nil
user=> (browse-url "http://clojure.org")
For a more involved tutorial, take a look at our 20 minute intro (labrepl based).
Also, there are some great video intros to the language itself found on the Clojure blip.tv site. Be sure to check out Clojure for Ruby Programmers, Clojure for Java Programmers, Clojure for Lisp Programmers, and Are We There Yet?.