Welcome to Programming!

👋🏾 🎉 💖

Why Clojure?

What is Clojure good at?

What does Clojure look like?

Comments

What is a REPL?

REPL in action

Why Clojure?

If you’ve never programmed before, you may not know that there are
many languages to choose from. Some of the other languages you might
have heard of (or will hear of!) are C, JavaScript, Python, and
Java.

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

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 all-purpose

Clojure is all-purpose. Some languages have a specific focus.
JavaScript, for example, was traditionally used only in web pages
(although that’s changed somewhat). Objective-C is used mainly for
iPhone apps. We’re going to make a drawing application today, but
you can use Clojure for any sort of application easily.

Clojure is fun

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.

What is Clojure good at?

So, we said Clojure is all-purpose, and it is. That doesn’t mean it
doesn’t have strong suits, though.

Data processing

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.

Concurrency

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.

Everything!

Clojure also works well for building drawing applications with
Quil, which is what we’re going to
do together.

What does Clojure look like?

(+ 3 4)
(max 8 17 2)
(print-str "Hello, World!")

Parentheses

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.

Functions

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.
+, max, and print-str are all functions.
When these functions get run, they return a some type of value.
Clojure functions always return a value.

Arguments

Many functions take in arguments (everything else inside
the enclosing parentheses function).

+ takes 3 and 4, adds them, and returns 7.

max takes 8, 17, and 2, and returns the highest: 17.

print-str takes “Hello, World!” and prints it out.

Comments

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.

What is a REPL?

“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

REPL in action

Nightcode’s REPL

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.

Using the REPL

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.

Evaluate program and line

Nightcode also lets us evaluate a line or an entire program at a time.
In the big panel, navigate to the src/(project_name)/main.clj file.
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 Reload Selection button
(ctrl/cmd + shift + r), making sure the cursor is next to the code.