Clojure Journey V – Simplicity, Evaluation and Syntax

Most of the time that I heard someone talking about Lisp family languages, probably was something like “that ugly language, why people program with that?”, “why so many parentheses?”, unfortunately since I started to learn Clojure, I hear it twice more.

This post is the fifth of a long journey on learning Clojure, and the first to really talk about code, but it can be served to learn about any language of the LISP family, this post will talk about syntax of the language, and the most important, we’ll understand why LISP languages look like this, the philosophy behind.

When you learn every language, most of the books in the first chapters is like a cake recipe, a long and boring list of all statements of the language, control flow, loops, var declarations, and much more.

Back to the day that you started to learn programming, probably you started with simple math and strings:

1 + 1;
=> 2
"Hello World";
=> "Hello World"

After that you learned about variables:

var age = 23;

Some control flow:

if (10 > 5) {
  "Hello World"  
}

And the most strange thing in every language, the for loop:

for (var i = 0; i < 10; i++) {
  console.log(42)
}

Probably you’re questing yourself about what I’m talking about, but have you looked at this common languages statements and noticed that they didn’t follow any logic or consistency in the syntax? The traditional for statements is not simple, we’re just used to view it all day. These syntax is not simple, and that’s the point.

Simplicity

In LISP family languages all the code follows the same logic, and after you’re capable of understand one expression, you’ll be able to understand the entire language. It doesn’t have many special forms, or something different from the rest, and that’s why “simplicity”is one of the worlds in this post title! Let’s take a look at some Clojure code, starting from the classical:

=> (println "Hello World")
Hello World

Let’s see some arithmetic:

=> (+ 1 2 3 4)
10
=> (= 1 2 3)
false
=> (+ (* 2 2) (* 3 3))
13

Can you imagine how if looks?

=> (if (> 2 1) "Greater" "Smaller")
"Greater"

What all these expressions have in common? They follow the same pattern. Let’s take a look:

LISP family syntax

In the image, the expression xxx is read as a list, the first element can be called “function position”, and the rest as arguments. When evaluating it will always try to evaluate the first item as a thing to invoke (you can think like a function, but sometimes can be other things like macros), and the rest as arguments the invoked.

Positioning the “function” before its arguments it’s called prefix/polish notation, the expression (f x y) looks a lot like the traditional mathematics notation f(x, y). All lisp expressions use polish notation.

Evaluation

During the process of evaluating expressions, it will evaluate each argument of the expression and then invoke the function of the first position with the result of evaluating each argument, in other words, it will evaluate all expressions from the innermost to the outermost recursively, forming a evaluation tree. Let’s assume (+ (* 2 2) (* 3 3)) as our example:

Evaluation Tree

For those who already studied programming languages design probably realized that this evaluation tree looks a lot like an AST (Abstract Syntax Tree), and it’s kind off.

You may have noticed that the numbers in the bottom of the evaluation tree are evaluating themselves, and it leads us to another important topic.

Different from other languages, Clojure doesn’t have statements, it has only expressions that evaluates to a value, and its programs is composed of n expressions. Every form not handled specially by any macro or special forms is considered a expression by the compiler and it will be compiled and has it’s value returned.

Most literal Clojure forms evaluate to themselves, except symbols and list, that’s why numbers was evaluated to themselves in the evaluation tree image. Other common examples of forms that evaluates to themselves are, booleans, strings, characters and keywords. Symbols are resolved based on a set of rules, but in general, it will look for what was “binded” to the symbol.

=> "String"
"String"
=> 42
42
=> :keyword
:keyword

It will try to evaluate everything, but we have a way to delay this process and say “don’t evaluate it, just return it raw”, just put an apostrophe before your expression:

=> '+
+
=> '(+ 1 2)
(+ 1 2)
=> 'variable
variable

Syntax

If you read the text so far, probably you already know how to write an expression and how literals are evaluated, you also know how to delay a evaluation using the apostrophe, now we can talk about one of the most interesting thing in LISP family languages, the “code as data and data as code” foundation.

Languages that are from LISP family, have a data structure called list, lists are basically zero or more forms enclosed in parentheses (1 2 3 42) is a example of one list with integers 1, 2, 3 and 42, and now comes the plot… Other examples of list is (+ 1 2 3) or (println "Hello World").

OMG! It’s a LIST

All the programs are composed of lists, and LISP/Clojure is defined in terms of evaluate its own data, and not a stream of keywords like other languages. LISP stands for LIST-Processing, and it explain this name.

Languages that have its programs represented by its own data are called “homoiconic”, and LISP family languages are one of them, based on this the affirmation “code is data and data is code” emerges and it is commonly used when talking about Lisp/Clojure.

When writing Clojure probably you’ll use lists as a data structure to store data, but if you try to use it to store a list of integers, probably you’ll get error during the evaluation proccess:

=> (1 2 3 4)
Execution error (ClassCastException) at user/eval2018 
(REPL:1)
java.lang.Long cannot be cast to clojure.lang.IFn

If we take a look at the error, java.lang.Long cannot be cast to clojure.lang.IFn, this error is saying that a integer can’t be cast as function and this error occurs because we tried to evaluate a list without any function in the first position, by default when evaluating any list, the Clojure reader tries to find a thing to invoke in the first position, that’s why when evaluating a list just as data structure, clojurists use the apostrophe to tell Clojure to not try to evaluate it:

=> '(1 2 3 4)
(1 2 3 4)
=> '(+ 1 2 3 4)
(+ 1 2 3 4)

And that’s the way that you usually will see programmers using lists as data inside Clojure programs, in a future post we’ll talk about how to use it.

Let’s finish our study about syntax with an experiment, Clojure has a function in it’s core named eval, if we pass a valid list, it will evaluate it:

(eval '(+ 1 2 3))
=> 6

This is the core of the REPL, and with Clojure simplicity, we can write our own in a simple way:

(loop [] (println (eval (read))) (recur))

Combining a function to read user input read, one to eval this input eval, print the result of evaluation with println and finally loop infinitely doing it. Run it and you’ll have your own read-eval-print-loop (REPL).

Invoking REPL

Conclusions

This a long post but essential on our journey, we ha been introduced to Clojure syntax and the logic behinds it. We learned about the famous “code is data and data is code” jargon, beautiful behind lists and built our own REPL.

LISP/Clojure syntax is beautiful and simple, it can afraid people because it’s different from what we’re used to, but if we study we start to understand it fast!

Now that we know the syntax, we can take a look in data types on the next post, and after that advance to functions. Some topics are missing in this post like special forms, but in a future post we’ll talk about it.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Useful Links

Evaluation, Clojure docs

The Reader, Clojure docs

Syntax, Clojure docs

Homoiconicity, Wikpedia

Polish Notation, Wikpedia

S-expressions, the Syntax of Lisp

Lecture 1A: Overview and Introduction to Lisp

Why code-as-data?

Hitchhiker’s Guide to Clojure

Clojure Journey II – Chose a editor

My second step through my journey to learn Clojure is to setup my text editor or IDE. I’m not a evangelist of any text editor or IDE, so I started to search for what people usually use or is best to use while coding Clojure. After few minutes and several posts around the internet, I noticed the four most indicated tools:

I started to try each one.. First I removed Cursive + IntelliJ from my options because cursive is a paid plugin, and I didn’t tried it at all. Then I tried to install Nightcode, it looks amazing, but I don’t like to use IDEs, is my personal option and in a few cases I chose to use IDEs (Java and Go).

Update: @tanrax made a great comment in this post saying that Cursive plugin is free for open source contributors and students, if you’re one, check it!

Now I have two rivals text editors and I need to chose one. I started to take a look at fireplace plugin to vim, because I use my neovim on 80% of my hours of coding, but after installed it, I faced problems when trying to fix them.

Finally I decided to take a look at Emacs (Yes, it was my last option), I never tried it before because it looks scary at the first time. I’m learning a new language, why don’t learn a new editor? I give a chance to emacs and…. It looks amazing!

In this post I’ll describe my setup and how to use emacs to development with Clojure.

Emacs or Spacemacs?

If you already use emacs and have your custom setup you can skip this part aand use your setup.

But what about those who don’t use emacs?

Emacs or Spacemacs is a long discussion, and I don’t want to get on one side now. Simplifying, spacemacs is a “comunity-driven emacs” and it comes with a lot of configuration already made. If you chose to proceed with emacs you’ll spend a time searching for plugins and custom setups that are already done or easy done with space.

As I am a beginner user with emacs, I decided to proceed with spacemacs.

Installing Emacs

Before install spacemacs, first we need emacs installed.

To install emacs is very simple and they have a nice webpage with all the links needed. As a GNU/Linux user I just downloaded the tarball file of version 27.1, extracted it and compiled/installed it from the source using the traditional ./configure, make and make install. After this emacs is installed!

$ emacs --version
GNU Emacs 27.1
Copyright (C) 2020 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file 
named COPYING.

If you have any trouble, you can see the INSTALL file that come with.

Installing spacemacs

Spacemacs have a beautiful website that need to be checked, but it has a nice README on github too.

As the date of this post, install spacemacs is very easy, just clone it in the correct directory:

git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

After this if you start your emacs, it should open spacemacs, and it will start to self setup. It will ask you some questions, but I chose the default one for every question, and selected the vim mode, to use vim bindings inside emacs. It helped me a lot, because I’m used to use vim.

Custom setup

Now you have a file named .spacemacs on your user directory, and this file is your configuration file, similar to other text editors.

The first thing that I noticed when I started it for the first time was the size of the font, it looks very small on my monitor. To change this is easy, open your .spacemacs file (Using your spacemacs, right?), and change your size:

dotspacemacs-default-font '("Source Code Pro"
                               :size 19
                               :weight normal
                               :width normal
                               :powerline-scale 1.1)

After this next time that you open spacemacs, your text size will be better.

Other thing that I missed from vim/nvim was the famous NERDTree plugin that is a file system explorer. Fortunately I found a layer named Neotree that is basically the same thing.

To install it is simple, as the documentation says, just add the layer name neotree to the dotspacemacs-configuration-layers list.

dotspacemacs-configuration-layers
   '(
     helm
     emacs-lisp
     neotree
     )

And if you want to open the tree pressing one ke, you can paste the fallowing code inside your config file:

(global-set-key [f8] 'neotree-toggle)

And if you like to have a system explorer looking like the vim’s one, just put this in your config file:

(setq neo-theme 'nerd)

And not, just reload your emacs:

The the only thing missing in our editor now is the Clojure support, and to install it is simple, just go to the same place where you installed the neotree layer, and write “clojure”, re-open your editor, and it’s done! The Clojure support for emacs is amazing and we’lll see in the next post!

What is spacemacs layers?

The first thing to clarify is that layers is a concept from spacemacs and not from emacs.

A layer can be defined as a collection of packages and it’s configuration required to make them work well with spacemacs and each other. You can easily find a open-source layer for almost everything that you’ll need (almost), but of course, you can make private layers too.

To install a layer is easy, just follow the same steps of this post: Add its name to dotspacemacs-configuration-layers list. You can check it’s documentation to learn how to use them.

Conclusions

In this post I chose my text editor that I’ll use when coding Clojure. On the next post we’ll talk about CIDER, The Clojure Interactive Development Environment for Emacs! And spoiler: It’s amazing.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares@gmail.com), pm me on my Twitter or comment on this post!

Clojure Journey I – First Steps

This week I started my journey through learning Clojure. Sometime ago I’ve a brief view of LISP when I read the land of lisp book, I was very interesting, for me it’s a language with a simple concept, that on the first view looks ugly and hard, but when you understand more it’ts fabulous and genius.

Clojure is a LISP dialect hosted on JVM created by Rich Hickey It first version was published in 2005 and today it’s not one of the most popular languages, but it have a nice and great community around, working on tools, library and documentation.

If you want to learn more about Clojure and its design decisions, Rich Hickey wrote a nice (and long) paper, about its features, design decisions and history, just click here.

My first steps was to chose a book to start learning, after a few search on google, I decided to follow the famous book Clojure for the Brave and True it looks a nice book and it’s free, if I like it I can buy a physical copy to reward the author.

As my second step I like to get involved with the community, I started to follow the Clojure subreddit /r/Clojure, and the newsletter listed here.

As my next step I’ll setup my code editor, but this is the subject of the next post.

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares@gmail.com), pm me on my Twitter or comment on this post!

Follow my blog to get notified every new post: