So why are we teaching Clojure? Although it’s not as popular as any of those languages, we’re using Clojure because of three qualities it has that make it an ideal first language to learn–or a great language to learn in addition to others you might already know:
Clojure is simple. That’s not to say it’s not powerful; it is. The number of concepts you have to know to program in Clojure is very small, however, and easy to grasp. Clojure grows with you as you learn it, and you can be very productive with a small subset of the language.
Clojure is fun. That’s a matter of opinion, of course, but we think it holds true. I hope that during this course you experience the joy of seeing a Clojure program come together and do something powerful and surprising.
So, we said Clojure is all-purpose, and it is. That doesn’t mean it doesn’t have strong suits, though.
Clojure is known for being good at data processing. That’s because it has a good set of data structures–that is, it has several built-in ways to represent data that are easy to use and powerful.
Clojure is known for its concurrency. Think about writing instructions for four of your friends about how to assemble a treehouse, but instead of writing them so one step is done at a time, each of your friends does part of the job. Then, they coordinate at the right time to assemble those parts into bigger parts, and they do this over and over again until the end, when it all comes together. Those instructions would be really complicated and hard to write–and probably hard to read, too. Clojure gives us some easy ways to write these sorts of instructions for computers.
Clojure also works well for building drawing applications with Quil, which is what we’re going to do together.
(+ 3 4) (max 8 17 2) (print-str "Hello, World!")
Notice the parentheses. Parentheses enclose instructions to the computer in Clojure. A left parenthesis is the start of the instruction, and a matching right parenthesis is the end of enclosing instruction. Normally, Clojure code has a lot of nested parentheses, on other words, nested enclosing instructions.
Next to the parentheses, we see the instructions to the computer. That instruction is normally what we call a function. The functions do all the hard work in Clojure.
print-strare all functions. When these functions get run, they return a some type of value. Clojure functions always return a value.
Many functions take in arguments (everything else inside the enclosing parentheses function).
+takes 3 and 4, adds them, and returns 7.
maxtakes 8, 17, and 2, and returns the highest: 17.
print-strtakes “Hello, World!” and prints it out.
When we write code, we try to make it as clear as possible. Doing so is a huge advantage because our code gets read by others (oftentimes more so than by us!), or we come back to our own code to read it later, by which point we may have forgotten each exact detail of the code. One way that we can clarify our code is annotating it with comments. Comments are notes that we add to code, for our own sake, that the computer ignores.
In Clojure, comments can be started with a semicolon. Everything after a semicolon until the end of that line is a comment that gets ignored by the computer. Only one semicolon is necessary, but sometimes you see two semicolons in a row, depending on stylistic tastes.
;; example functions from a previous slide (+ 3 4) ; why not 3 + 4? figure out later (max 8 17 2) ; there's a min too (print-str "Hello, World!") ; a well-known hello world
“REPL” stands for “Read-Eval-Print-Loop,” which still doesn’t make a ton of sense without context. Many programming languages, including Clojure, have a way to execute code interactively so you get instant feedback. In other words, the code is read, then it is evaluated, then the result is printed, and you begin again–thus, a loop.
Read, Eval, Print, Loop
To interact with Clojure, we’re going to be using the REPL tab in Nightcode. It’s a nice way to play with Clojure interactively.
There’s a couple of ways to get to a REPL in Nightcode. One general one that is always running in the corner of the window, and another that is launched with the “Run with REPL”.
The easiest REPL to access and use is loaded from the beginning of opening Nightcode. You can find it in the bottom left hand window pane.
Nightcode also lets us evaluate a line or an entire program at a time. In the big panel, navigate to the
/main.cljfile. After pressing “Run with REPL”, you should see some output in the bottom pane. The first time you run this, it’ll take a little while.
After this, you’ll find a new REPL launch in that bottom pane. Here you can type commands just like the other REPL – but now you can also type code in the main file, and run it with the
Evalbutton (ctrl/cmd + shift + x, making sure the cursor is next to the code).
(print-str "Hello, World!") (print-str "Hello, World!" " " "from Clojure") (+ 3 4) (- 3 4) (* 3 4)
Make sure you type the lines exactly as you see them above, taking care to put the parentheses in the right locations.
(fill 192)and change it to
(fill 250 20 20)
Eval Selection(ctrl + E or cmd + E)
(clojure.repl/doc function-name)command to do this. For instance, try typing
(clojure.repl/doc fill)into the REPL