Commit 54f5bf7a authored by Christian Weilbach's avatar Christian Weilbach
Browse files

First round of rephrasing and feedback.

parent de91de39
......@@ -5,46 +5,47 @@ draft: true
tags: ["clojure","programacion funcional", "tutorial"]
---
A few weeks ago I returned to study Clojure after many years. Some time ago a friend introduced me to this language with a very different paradigm than what I knew programming in C ++, PHP and Python. At that time it was complex and frustrating for me to understand it, especially due to the little documentation that I had access to. Today, thanks to the greater information available, I returned to the task of approaching this paradigm of Functional Programming. I remember when I was at school someone told me once ...
After many years I returned to study Clojure, http://clojure.org/ a few weeks ago. Some time ago a friend introduced me to this language with a very different paradigm than what I knew by working with imperative, object-oriented programming in C++, PHP and Python. At that time it was complex and frustrating for me to understand it, especially due to the little documentation that I had access to. Today, thanks to the greater information available, I returned to the task of approaching this paradigm of Functional Programming. I remember when I was at school someone told me once ...
> **They say that when you are learning something new the best way to fix the concepts is to try to explain to another. And the best way to teach another is when you still remember how difficult it was when you didn't know anything.**
> **They say that when you are learning something new the best way to fix the concepts is to try to explain it to somebody else. And the best way to teach somebody is when you still remember how difficult it was when you didn't know anything.**
That is why today I decided together with my learning process to also create a series of mini tutorials with basic concepts to learn clojure.
That is why today I decided together with my learning process to also create a series of mini tutorials with basic concepts to learn Clojure.
## What is clojure?
## What is Clojure?
Clojure is a functional programming language, a LISP dialect, that runs on the JAVA virtual machine, from .Net and can be compiled to JavaScript (ClojureScript). Running over JVM (JAVA virtual machine) makes calling JAVA libraries very easy, which makes it more powerful. It stands out for being efficient and having an immutable data structure.
Clojure is a functional programming language, a LISP dialect, that runs on the Java virtual machine (JVM) including its successor the Graal virtual machine (https://www.graalvm.org/), on the CLR (.NET) including Unity3D https://arcadia-unity.github.io/ and can be compiled to JavaScript (ClojureScript). It also has popular community ports for Erlang, https://github.com/clojerl/clojerl, and an efficient interpreter for the Unix shell https://github.com/borkdude/babashka/. By running on the JVM Clojure deliberately makes calling Java libraries very easy, which gives it more reach than other functional programming languages. You can be pragmatic and easily use established solutions and frameworks in the Java ecosystem when you need it. It stands out for being build on innovative immutable data structures and being fairly efficient.
Functional programming is a declarative programming paradigm based on the use of mathematical functions, in contrast to imperative programming, which emphasizes state changes through the mutation of variables. Functional programming has its roots in lambda calculus, a system formal developed in the 1930s to investigate the definition of function, the application of functions, and recursion. Many functional programming languages ​​can be seen as elaborations of the lambda calculus.
Functional programming is a declarative programming paradigm based on the use of mathematical functions, in contrast to imperative programming, which emphasizes state changes through the mutation of variables. Functional programming has its roots in the lambda calculus, https://en.wikipedia.org/wiki/Lambda_calculus, a system formal developed in the 1930s to investigate the foundations of computation. It builds on the definition of functions and their application including recursion. It has a very small core without number primities, yet has proven to be Turing-complete. Most functional programming languages ​​are elaborations of the typed or untyped lambda calculus.
## Functional programming
If you are interested in writing programs with more robust code, less susceptible to errors and expanding your thinking; functional programming has a lot to offer you. For example, the immutability minimizes the possibility of finding defects originating from the manipulation of states in unknown places.
If you are interested in writing more robust programs, that are less susceptible to errors and expand your thinking; functional programming has a lot to offer to you. For example, the immutability minimizes the possibility of introducing accidental defects originating from the manipulation of the program state in unknown places, e.g. libraries. Immutability reduces complexity and allows to reason locally.
Another great reason to learn functional programming is take advantage of parallelism. Writing parallel code in a functional paradigm language is very easy.
Another great reason to learn functional programming is to take advantage of parallelism. Writing parallel code in a functional paradigm language is very easy.
For some people, the mathematical origins of functional programming make this paradigm simpler to be understood than object orientation. For these people the idea that whenever a function is called with the same parameter, the result will be the same, without anything else in the program universe being able to change. Of course This is not exclusive to functional programming, since we can achieving the same result in other paradigms, but functional programming will help you always think about building works without side effects.
For some people, the mathematical origins of functional programming make this paradigm simpler to understand than object orientation. They build on the mathematical concept of functions, whenever they are called with the same parameter, the result will be the same, without anything else in the program universe changing. Of course this is not exclusive to functional programming, since we can achieving the same result in other paradigms, but functional programming will help you always think about building works without side effects and standardizes functional reasoning easy in all libraries.
NOTE: I would recommend to add some links to books and background material about functional programming and maybe the Lambda Calculus here.
## What do I need to program in Clojure?
To program in clojure the requirements are simple:
To program in Clojure the requirements are simple:
* Java JDK version 11 virtual machine (OpenJDK on most GNU / Linux systems)
* Java JDK version 11 virtual machine (OpenJDK on most GNU / Linux systems) or later (and probably earlier, the JVM is very stable and backwards compatible).
* Leiningen (you can find the installation instructions at http://leiningen.org/)
**Note:** in a next tutorial I will explain in detail the installation process of Clojure.
**Note:** in one of the following tutorials I will explain the installation process of Clojure in detail.
## Hello World!
To start working in Clojure, an acronym that will be common at all times is REPL (an acronym for read, evaluate, print and loop) which is an interpreter of the commands that we will be passing to it. If you are already a Ruby programmer, you may have already worked with irb or pry. Python also offers a tool for the same purpose. Scala and Erlang are examples of languages that also offer such tools.
To start working in Clojure, an acronym that will be common at all times is REPL (an acronym for read, evaluate, print and loop) which is an interpreter of the commands that we will be passing to it. If you are already a Ruby programmer, you may have already worked with irb or pry. Python also offers a tool for the same purpose. Scala and Erlang are examples of languages that also offer such tools by default.
The first step would be to open a console and type
The first step would be to open a console and type:
```
lein repl
```
In the interactive terminal should appear for you, with a message more or less like this:
The first time it will take a while to execute Leiningen, since it will need to download some dependencies to start. In the interactive terminal message more or less like this should appear to you:
```
nREPL server started on port 59100 on host 127.0.0.1 - nrepl://127.0.0.1:59100
......@@ -64,19 +65,19 @@ Pressing the Enter key (don't forget the "") we pass the instruction to the inte
```
Hello World!
```
You have not heard, but it seems to me the simplest way that exists to write the famous "Hello world" of any programming language (perhaps the first time it will take a while to execute Leiningen, since it will need to download some dependencies to start).
This seems to me the simplest way that exists to write the famous "Hello world" of any programming language.
## Basic operations
If I get this far then why not try a couple more operations inside our REPL interpreter?
If I get this far then why not try a couple more evaluations inside our REPL interpreter?
We are going to see some arithmetic operations of Clojure to understand a little more the syntax of it. No REPL, type the following operation:
We are going to see some arithmetic expressions of Clojure to understand the syntax of it a little more. Type the following expression into the REPL and hit ENTER:
```
(+ 2 3)
```
The result, as you probably already saw, was 5, but the syntax seems very different from everything we know in other languages.
This line looks like a list (keep this concept in your memory that we will return to at another time), made up of +, 1 and 2, and it is the same; but we go for part:
The result, as you probably already saw, was `5`, but the syntax seems very different from everything we know in other languages. https://en.wikipedia.org/wiki/Polish_notation . While this looks crazy, it is a bug not a feature. http://www.paulgraham.com/onlisp.html
This line looks like a list (keep this concept in your memory, we will return to it at another time), made up of +, 1 and 2; Let's look at the parts:
> \+ ==> Function (sum operator)
......@@ -84,11 +85,10 @@ This line looks like a list (keep this concept in your memory that we will retur
> 3 ==> 2nd Argument
all this in parentheses.
It happens that the first item in this list is special. This element is a function that is executed, and the other elements in this list are arguments for this function.
It happens that the first item in this list is special. This element is the function that is executed, and the other elements in this list are arguments for this function.
The same can be found in other simple and complex arithmetic operations:
The same can be found in other simple and complex arithmetic expressions:
```
(* 2 8)
......@@ -109,13 +109,13 @@ Something a little more complex:
Here it would be important to introduce two concepts:
1. In clojure to enter comments to the code we use two semicolons followed in this way ;;
1. In Clojure comments start with one or two semicolons: ;;
Everything behind this double score will be ignored by the interpreter. For example
```
;; We already know how to perform arithmetic operations in Clojure
```
2. Operations in clojure always go from the inside out in the same order of the arguments and respecting the parentheses.
2. Expressions in Clojure always evaluate from the inside out in the same order of the arguments and respecting the precedence of the parentheses.
For example:
```
......@@ -133,9 +133,11 @@ For example:
* Subsequently, the subtraction is performed 10 - 90 = -80
This example is where Clojure shows another difference in the syntax sense. From the point of view of precedence, it is clear that the sum will be done first. The differential, however, is that the language requires parentheses, which leaves no doubt as to what precedes what, therefore, the order of execution of the code is always from the inside out.
This example is where Clojure shows another difference syntactically. From the point of view of precedence, it is clear that the sum will be done first. The difference, however, is that the language requires parentheses, which leaves no doubt as to what precedes what and therefore, the order of execution of the code is always from the inside out.
It is here I hope that some basic concepts of Clojure have been made clear.
NOTE: Actually expressions in any language are evaluated that way. If you write (10 - (10 * 9)) in Python then it will also evaluate from the inside out. It is the nature of composing expressions that has this inside out character. In many functional languages expressions can be sequentialized by let or monadic bindings.
It is here I hope that some first basic concepts of Clojure have become clearer to you.
## Closing our interpreter
......@@ -152,4 +154,6 @@ It is also possible to exit with the key combination control + d.
## To end
I hope you found this first tutorial of several interesting that I plan to publish. I await your impressions, doubts and comments. I'm going to share some arithmetic operations with which you can exercise and share the results in the comments.
\ No newline at end of file
I hope you found this first tutorial interesting. I plan to publish more tutorials while I am learning Clojure. I am looking forward to your feedback, criticism and comments. I'm going to provide some arithmetic operations with which you can exercise and will share the results in the comments.
NOTE: Can you already say something about the topics you plan to cover next?
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment