Clojure

I am trying to learn clojure, therefore the best way that I know how to learn something is to write about it. This allows me to digest the things that I am learning. Apparently I had forgotten about this tactic for the past couple weeks, as I was trying to just read and do problems to help learn this. Therefore lets get started.

NOTE:

I am writing this as a way to digest and understand the book that I am working through entitled. Living Clojure

What?

What is clojure and why do I care? Clojure is a lisp that is sitting on op of a JVM. It is a functional programming language. What is a functional programming language? …functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements which change the state of the program or world. https://en.wikipedia.org/wiki/Functional_programming Functional programming is a real mind job if you are coming to FP from object oriented programming. This is what I am doing and it is real hard to make my brain think this way. However if you have already dabbled in say terraform some, then the declarative world may not be completely new. So we have that going for us.

Getting started

Once we have clojure installed follow whatever install guide you would like. https://clojure.org/guides/getting_started or brew install clojure/tools/clojure We will now have the ability to launch a clojure REPL : read–eval–print loop. This acts much like a python interpreter or even a command line. We will start there.

$ lein repl
...
user=> 42
42
user=> (+ 1 1)
2

I typed in just the number 42 and hit enter, this gave me an integer back out, cool easy enough Next I did a basic function call (+ 1 1) which added 1 and 1 together, then it returned 2. Easy enough although that syntax is super weird, but that is just the world we are living in.
What else is weird? ratios Ratios are not broken down to decimals The “mathing” will be done if it can result in integers, but anything else it won’t do. For example:

user=> 1/3
1/3

user=> 4/2
2

user=> 13/1
13

user=> 13/2
13/2

user=> 6.0/3
Syntax error reading source at (REPL:1:1).
Invalid number: 6.0/3

user=> (/ 1 3)
1/3

We can see that the 1/3 stays 1/3 instead of .3333 Additionally we cannot mix and match decimals and whole numbers for the ratio/math, it will give an error. Even if we go as far as doing the function call for the (/ 1 3) it will still return a ratio. We can finally get it to return some repeating decimals with:

user=> (/ 1 3.0)
0.3333333333333333

yey!!

Strings and others

Strings are pretty easy we just type in a word "string" and it gives us a string Additionally keywords are done with :myKeyWord Booleans are the same way true and false :w

Collections

Here we are getting in to the something more interesting Collections! List collection:

user=> '(1 2 "face" :something-else)
(1 2 "face" :something-else)

We can ignore using commas or we can use them. The ignoring of the commas is known a idiomatic.
What is idiomatic? Using, containing, or denoting expressions that are natural to a native speaker. ‘distinctive idiomatic dialogue’ https://www.lexico.com/en/definition/idiomatic Otherwise known as you can use commas but it is idiomatic not to, alright sure fine. We can access the first element of a list by using first <list> and then the rest of the list, or NOT the first with rest <list> simple right? Once we get to the end of the list, it will return a nil. We can build the list with cons <element-to-add> <list-name>

user=> (cons 5 `())
(5)

Alright that is where I am going to have to stop for tonight but tomorrow I will pick up with vectors.
Good luck

Links and notes: clojure cheatsheet - http://jafingerhut.github.io/cheatsheet/clojuredocs/cheatsheet-use-title-attribute-no-cdocs-summary.html