Your First Program
Prerequisites: Installing Clojure
Create a file named hello.clj:
(println "Hello, world!")
(println "Welcome to Clojure.")
Run it:
clj -M hello.clj
Expected output
Hello, world!
Welcome to Clojure.
(println "Hello, world!")is a list: the first item is the function to call, the rest are its arguments.- Strings use double quotes only.
- No semicolons, no
mainfunction: Clojure reads and runs a file one expression at a time, from top to bottom.
Printing several values
(println "Answer:" 42)
(println 1 2 3)
(println (str "no space " "here"))
(print "no newline")
(println)
(prn "prn shows quotes" [1 "two" :three])
Answer: 42
1 2 3
no space here
no newline
"prn shows quotes" [1 "two" :three]
printlnseparates its arguments with spaces and ends the line.strjoins values into one string.printdoesn't add a new line;(println)prints just one.prnprints values as data — strings keep their quotes — which is handy for debugging.
Expressions return values
(println (+ 1 2))
(println (* 6 7))
(println (str "Clojure " (+ 1 0) "." (* 3 4)))
3
42
Clojure 1.12
Everything in Clojure is an expression that produces a value; you nest expressions to combine them.
Practice
Write a program that prints your name on one line and your favorite food on the next.
Show solution
(println "Ada")
(println "Pizza")
Ada
Pizza