What Does This Mean
Alright, so you want to learn how to program. That's fantastic! Let's pull up a keyboard. No, I won't be doing the typing. You will!
So where do we start. We need to pick a programming language, that is the parlance both we and computers understand. There's an awful lot of them out there and people still invent new ones. It's easy to get overwhelmed. For starters, we want a language that gives us quick feedback, doesn't draw our attention to the irrelevant, and at the same time won't make us develop bad habits. Let's start with Clojure. It ticks off all the boxes.
Programming is all about communication. Be it with computers when they run our programs or—perhaps even more importantly—with other people, who will read and extend our code in future. Today we'll focus on computers.
Programming in Clojure boils down to having a conversation with the machine. I'll open a terminal window and start a thing called a REPL (it stands for read-eval-print-loop but don't worry about that for now). I know, it all looks very foreign, but don't worry. As software developers are used to not knowing things and turning unknowns into learning opportunities.
A REPL—pronounced like “rebel” but with a “p”—is where the conversation between us and the computer takes place. To open it type
clj in the terminal.
Clojure 1.10.1 user=>
So what it says is that we're in a Clojure REPL and it's waiting for us to type something. Clojure can give us answers to questions like “what is x?”, or rather “what does x mean for you?” Let's begin with a digit. What is 5? Press 5 and confirm with the return key.
user=> 5 5 user=>
Great, so we asked Clojure what does five mean. It told us it means the number five, and it's waiting for more input. We can keep asking for meanings of things. Let's take a different whole number, for instance 123. Try to enter some fractions, for example ½ or 75%, which we will express as 1/2 and 75/100 respectively.
user=> 123 123 user=> 1/2 1/2 user=> 75/100 3/4
Notice that in case of 75/100 we didn't get back the same thing we put inside. We asked “what does 75/100 mean for you” and Clojure responded with ¾. The REPL did some math for us and provided us with the simple value for which 75/100 stands. We'd say Clojure evaluated 75/100 into 3/4.
Those were individual numbers. Can we have more than one number at the same time? Say, counting letters in words of a sentence? Yes, we can. We do that by surrounding our numbers with square brackets. Let's type in the REPL. What does the collection of 1, 2, and 3 stand for? What is the value of two thirds and two fourths?
user=> [1 2 3] [1 2 3] user=> [2/3 2/4] [2/3 1/2]
Notice how two fourths got simplified into 1/2. That's evaluation again; giving us the value our input stands for.
For the sake of legibility we can intersperse our numbers with commas. They don't make any difference for the REPL, though. As you see, Clojure ignores them and replies with the result of evaluation.
user=> [5, 6] [5 6]
What if we want to add two numbers? To add numbers we use a plus, just like in math.
user=> + #object[clojure.core$_PLUS_ 0x6c2f1700 "clojure.core$_PLUS_@6c2f1700"]
Oh, that's confusing, but don't worry. It's also opaque to me. We asked the REPL what does
+ mean in Clojure and it replied with an answer that's not transparent to us. But try something else instead, namely:
user=> (+ 2 3) 5
Now that's exciting! Those round brackets—or parentheses—behave differently from square ones. Take a look.
user=> [+ 2 3] [#object[clojure.core$_PLUS_ 0x6c2f1700 "..."] 2 3]
I edited the last line for the sake of legibility. Let's compare how the REPL reacted to
(+ 2 3) and
[+ 2 3]. The one in square brackets was like
[1 2 3] above, but we replaced the first number with a plus. The answer from the REPL reflects that change. Instead of 1 there's that opaque thing.
In case of the round brackets the question we posed in the REPL has a different meaning. We aren't asking for the value of three things, that is
+, 2, and 3. We are asking what happens if you take
+ and apply it to 2 and 3. Let's try different numbers.
user=> (+ 1 1/3) 4/3
Enclosing things with parentheses makes Clojure take the first thing, assume it's a function, and treat remaining ones as arguments to that function. So in case of
+ the function is addition and arguments are the things that follow the
Function ⤵ ( + 1 1/3 ) 1st argument ⤴ 2nd argument ⤴
Try it with minus and see if it subtracts.
user=> (- 0 3) -3
The math checks out.
Go ahead, experiment. Try something else. Don't worry, you won't break anything. You see? You're programming.
Got any questions? Just let me know, I'm here for you.