Skip to content

Instantly share code, notes, and snippets.

@mpj
Last active November 1, 2015 19:28
Show Gist options
  • Select an option

  • Save mpj/d5702f524efdfa8942df to your computer and use it in GitHub Desktop.

Select an option

Save mpj/d5702f524efdfa8942df to your computer and use it in GitHub Desktop.

Welcome to funfunfunction!

Today, we’re talking about Clojure, more specifically the syntax of Clojure. I’m doing this episode bit hung over because yesterday was Halloween. I was at a party with scary tales theme. This is my costume, it’s the three bears from goldilocks.

So why look at other languages? We mostly use JavaScript in this show, because it’s the lingua franca of the internet, but we really don’t want to learn JavaScript, we want to learn programming, and to do that we need to expand our horizons.

Clojure is cool because it has almost no syntax at all. Clojure really is to programming languages what the Star Wars lightsaber is to weapons. Let’s compare Clojure to JavaScript:

1 + 2 + 3;
(+ 1 2 3)

When I saw this for the first time, it made me go apeshit. Why is the plus to the left? That MAKES NO SENSE.

This is because what we are looking at is almost the entire Clojure syntax. Almost all Clojure looks like this:

(operator operand1 operand2 operand3)

Open paranthesis, an operator, then as many operands as you like, and then a closing parantheis.

What? I’ll show you some more examples, and you’ll get it. Here is a variable declaration in JavaScript and in Clojure.

var myFerret = “Waffles”;
(def myFerret "Waffles")

We initalize the variable myFerret with the value “Waffles”.

In the clojure example, we have our operator, def (most definitely short for define), and then two operands follow, the variable name myFerret, and the variable value “Waffles”.

I’ll show you a third example:

true ? “You can have delicious soup. : “No soup for you!
(if true
	“You can have delicious soup.”
  “No soup for you!“)

See here, it’s the same bloody thing. Open paranthesis, the operator if, and then three operands. The first operand is the boolean being evaluated, the second operand is what is what we get if the boolean is true, and the third operand is what we get if the boolean is false.

Again, it’s always …

(operator operand1 operand2 operand3)

And this is called a form.

But where are my curly brackets? What if I want to do multiple things in my if statement?

if(true) {
	console.log(“You can have delicious soup.);
	console.log(“… and a puppy!);
}

(FACE) THE EXACT SAME WAY

(if true 
	(do
		(println “You can have delicious soup.”)
		(println “… and a puppy!”)
	)
)

So now you’re probably recognizing Clojure as that language with all the fucking paranthesis. But look at how elegant this is, there is no new syntax introduced here, the only new thing is that we’ve started nesting forms. So our outer form starts with the operator if, after that comes the first operand, the boolean being evaluated, and then comes another, nested form, with the operator do. The operator do will simply do all of the forms passed to it as operands, which is these two println forms.

And now you know 90% of the clojure syntax. It’s pretty cool because almost all other languages have tons of tons of language constructs, but in Clojure there is almost only these operators.

You have watched an episode of funfunfunction, a weekly show where we try to become more confident and excited about programming by exploring old wisdom, wild ideas and having fun. Don’t miss out on the next epsiode - follow me on Twitter @mpjme.

There will be no show next monday because I’m having a week off, so make sure that you follow me on twitter @mpjme so that you don’t miss out on the next one.

Stay curious!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment