### Created chapter 2 branch

revision c7a24b5e054682ff431599e976f1e030b283d8b0
A_Loose_Introduction
####A Loose Introduction

What is Clojure?

Clojure is a LISP dialect. A type of list processing language. It runs in the Java Virtual Machine (JVM).

If you’re reading this book you must have at least some casual interest. Whether you heard about it from a fellow nerd, Hacker News, Reddit, or a myriad of other proprietors of information, you have stumbled into what I would call “an interesting beast”; to say the least.

What is LISP?

Well, it is short for a List Processing Language, as previously alluded. Paul Graham put it well:

>“In 1960, John McCarthy published a remarkable paper in which he did for programming something like what Euclid did for geometry. He showed how, given a handful of simple operators and a notation for functions, you can build a whole programming language. He called this language Lisp, for "List Processing," because one of his key ideas was to use a simple data structure called a list for both code and data.”

>-Paul Graham “The Roots of LISP” -2001

What does this mean for you? Well, if you have never encountered a list processing language, here is a simple example of some LISP (and also, valid Clojure) code:

(+ 1 2 3)
> 6

Now, this may seem a bit odd at first. But let us break it down a bit. Clojure programs consist, at their core, of simple statements. In a brief step back, we could give the following examples:

(println (“foobar”))
>”foobar”

Simple expressions evaluate to themselves. Now, what is this business with throwing in the “+” symbol at the beginning, you may ask? It’s so counter-intuitive! Why!? Well, simply put, in a list processing language, everything is evaluated in the form of list. In Clojure, these lists are even immutable! Before you scream “That is just crazy!”, let us continue onwards. Let us examine our original example.

Initially, we see a group of some “stuff” in some “parentheses”. Let us dig deeper. We begin reading the inside of these “parentheses” (hereon referred to as a list) to find a “+” symbol. Now, a key here is the usage of the term symbol. In clojure, if one were to evaluate the following:

( + )

The output would be:

( 0 )

Why? Because in this list processing world, We evaluate lists in the order we get them. In the previous example of ( + 1 2 3 ) when we begin to process this list we first see the “ + “ piece of the list, we now know our operation of choice will be addition. What does the term “symbol” mean in these terms? Well, to put it concisely, this is due to the fact that is will not evaluate to itself (as shown in the previous example). Clojure (and other LISPs) possess this data type that few other languages have and/or utilize. This opens many doors. Due to the fact we can process an entire list in this manner, we can now perform arbitrarily long addition, such as:

(+ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
> 18

We can also nest other lists:

(+ 1 2 (- 5 2))
> 6

Stop for a moment. Draw out some lists and nest them. Try throwing in * and / operators as well. Try to grasp the power that is being laid in your hands with this style of processing your data. http://tryclj.com/ can provide you a wonderful interface to run simple Clojure code if you prefer not to write it out by hand.

Now, if you have parsed these basic concepts, I’m sure your mind is just running and running. So far we have seen the basic definition of a list, some addition, passing in one’s self to the list, and a very simplistic look at strings. Let us further examine what basic footholds Clojure has for us to grasp:

Strings
Integers, division, multiplication
and last, but not least…
function definition, the heart of functional programming. Strap in.

Strings:
Here are some simple examples of strings to look at.

( println “I like ducks” )
>”I like ducks”

( println “Missing area father found in lumber section of Lowes” )
>”Missing area father found in lumber section of Lowes”

( str “Eat ” “my ” “shorts.” )
>“Eat my shorts”

Now, the first two pieces of this are reasonably run-of-the-mill. However, those who may have cut and glued some strings together in days of their past may immediately recognize that that form of syntax for concatenation is far from what one is usually used to. For example, in Ruby, one would expect

puts “Eat ” + “my ” + “shorts.”
>“Eat my shorts.”

We will go more into this later, but it just further illustrates that Clojure will operate just a little bit differently than what you may be used to in other languages.

Integers:

Integers behave as you may expect...at first.

( + 1 2 3 4 (/ 2 2))
>11

(* 2 3 (/ 12 2))
>36

However, lets throw some nontrivial division at it.

(/ 2 3)
>2/3

This is new and different. Clojure has a built in rational data type. However, if we introduce a float to the list, we see this.

(/ 2.0 3)
>.66666666666666…

It will even handle multiplying and dividing these ratio type items.

(* 2/3 5)
>10/3

(/ 2/3 4/5)
>10/12

Now, we’ve served appetizers. Lets jump into the steak and potatoes of Clojure. FUNctions! At its core, functions in Clojure are much like any other language. Being a functional programming language, it best be able to make a lot of useful ones. Lets look at a simple example of a function in Clojure.

(defn square [x] (* x x)) 1

Now, when we call our “square” function, this will happen.

(square 10)
>100

Now, let us break this down. First off, let us continue to notice that everything is in lists, if we didn’t make that clear already. Here is a breakdown of the previous statement:

(define function [defn] function_name [square] function_parameter [ [x] ] ( function_definition ))

This structure is quite simple. Much like other languages, we now have an understanding of how these funky things we call functions work. Let’s compare it to some other languages.

Javascript:

function doIHavePantsOn(location) {
if(location === “home”){
return “no”
}else{
return “yes”
}
}

Ruby:

def do_i_have_pants_on(location)
if location == “home”
puts “no”
else
puts “yes”
end
end

Now, as you can see, functions really are the most similar to any other language when it comes in regards to Clojure’s syntax. They are its bread and butter. Clojure has many built in functions, too. But we will dig into those later.

This should give you a very basic grasp of what Clojure is and how it can be utilized. Take the examples here and try some exercises.

###Exercises 0.1

To do these, go to www.tryclj.com

1. Write a function that will take a number in and cube it

2. Write a function to combine two strings into a sentence, separated by a comma

3. Try to concatenate an integer or float and string and see if this means of concatenation works different than in Ruby or JS, or whatever language you may be used to.