Pros and cons of the functional paradigm and Haskell

I have seen that clearly the functional programming paradgima is nothing famous in the world of programming in general. However, chatting with a few fans of languages like Lisp, Ocaml, F#, Haskell, etc. I saw arguments that almost convinced me to learn the purely functional language that seems to be the most influential and popular of the moment: Haskell.

So the question Is Fundamentally this: What are the pros and cons of functional programming? What are the pros and cons of Haskell? Why should I learn Haskell?

Just to leave a clue, one of the arguments I heard the most from fans of the functional paradigm was: "Parallelism and threading in functional programming is almost trivial from so simple, while in other languages this is difficult, arduous and due to poor implementation only brings problems".

Author: mgibsonbr, 2014-04-17

4 answers

To tell the truth, I think the term "functional programming" is not very accurate, and I would prefer to separate Lisps from ML-style typed languages (Haskell, Ocaml, F#, etc). That said, to answer the question "Why learn Haskell", I think it is better to divide into two parts.

  1. Functional Programming in general

    One thing that all functional languages have in common is the way functions are first-category values: you can define functions within other functions (accessing function variables from outside), define functions that receive other functions as a parameter or that return functions... This is quite flexible and allows you to write a lot of things succinctly.

    Nowadays, this point of functional programming is no longer such an important distinction, since many "imperative" languages have first-class functions and even languages notable for the lack of anonymous functions, such as Java and C++ are finally including them. And since we talk about imperative programming, to tell the truth, functional programming doesn't even have to be that different - you can write programs with a rather imperative face if you use tail recursion (something equivalent to computed gotos).

  2. Languages of the ML family (Haskell, Ocaml, etc)

    The most unique factor of Haskell and its raw languages (Ocaml, F#, etc) is certainly the type system, extremely powerful and flexible, but still relatively easy to use. I would say that it is worth learning such a language just for that. Some of the most important points:

    • The type system is completely static. Many errors that are detected at runtime in other languages are detected at compile time in Haskell.

    • Variables will always be initialized (cannot access an uninitialized variable).

    • Polymorphism parametric / generics. It allows you to express precise types, without having to upcasting and downcasting with an object supertype.

    • Tagged unions and pattern matching. It is possible to describe types with more than one case (for example, "integer value or null") and it is easy to write programs that deal with all these cases (pattern matching is a mix of switch with unstructure). This all avoids exceptions like NullPointerExceptions, since the type of a nullible variable is not the same type of a non-nullible variable.

    • Type inference is powerful and if you want you can write any program without using any type annotation, which avoids hassles like MyClass myClass = new MyClass. That said, it's still a good idea to put some type notes here and there to make the error messages more understandable.

  3. Haskell, specifically

    • Haskell is one of the functional languages with the most active. It will be easier to find documentation, libraries and introductory books for Haskell. In particular, Haskell has some really cool libraries, which are not common in other languages:

      • Quickcheck . Generates random entries for your test cases automatically, based on function types.

      • Hoogle . Searches the standard Haskell Library based on types. Way extremely fast and effective to search for functions if you know what you want them to receive and return but don't know their name.

      • Parser Combinators . A flexible way to write your own parsers but one that would be difficult and ugly to implement in conventional languages.

      • Haskell uses lazy evaluation, which makes it easy to define your own combiners and flow control structures. For a slightly forced example, we can define

        MeuIfThenElse cond ifTrue ifFalse = if cond then iftrue else iffalse

      In Haskell, this function behaves exactly like an if. Already in a language with strict evaluation, this function would not work as well, since the two parameters ifTrue and ifFalse would always be evaluated, rather than a single being. You would need to explicitly wrap them in some extra functions:

        function meuIfThenElse(cond, onTrue, onFalse){
            if(cond){ onTrue() } else { onFalse() }
        }
      
    • Due to lazy evaluation, Haskell ends up having to separate functional code without side effects from code with side effects (which need to be executed in a precise order). A lot of people find this confusing at first (having to use monadas to print and read values) but after you learn how it works it is very useful to have the type system marking which parts of the program can have side effects and which are pure. If a function is pure you can call it without having to worry about when it runs or if it will change some instance variable of an object of yours.

 19
Author: hugomg, 2014-08-23 13:36:55

Have already been given good answers, but still missed answering some questions of the OP, which I will try to deal with here

What are the pros and cons of functional programming [regarding parallelism and threading]?

The question here is not so much the functional vs. imperative, but rather mutability vs. immutability.

Imperative languages tend to make great use of changeable structures and objects, with state change. A property of a object, encapsulated by a pair of methods get and set is an example of this.

This style of programming is often simple, in a single-threaded environment, but gains orders of magnitude of complexity when considering multi-threaded environments. Because, if more than one process can read and change a variable, it is necessary to control the simultaneous read and write accesses to memory (through the use of traffic lights, for example example). And this control, because it is very complex, is also very susceptible to problems such as deadlocks, low performance and other implementation errors.

Functional languages tend to use only immutable structures and objects. This means that created objects are never changed again (if an information needs to be changed, this implies creating a new object, and not changing the old one).

This style is much more expensive, from the point of view of memory consumption, but it has one big advantage: all the difficulties associated with controlling memory reading and writing cease to exist (but, of course, some new difficulties also arise).

Also comes from this, in part, the predilection of functional languages towards lists , because they are structures that can be easily manipulated, without giving up immutability (it is possible to "add" an item to a list simply by creating a new "head" that points to the old list - that is, a new list object is created, but without having to copy the items from the old list to the new one, saving processing).

Why should I learn Haskell?

That's a tricky question, as I Don't think you should learn Haskell (at least not now)!

You (the OP) don't seem to have much knowledge about functional languages. And Haskell is certainly one of the most complex and difficult programming languages to learn. In addition to immutability, you would have to learn such concepts as lazyness, purity, type-classes, monads, monoids, transactional memory... And, believe me, this is all even stranger than the fact that it is functional. : P (in my opinion, it's more or less like wanting to learn quantum physics, without having learned classical Newtonian physics: it's possible, it's just not sensible).

There are many more options accessible:

  • Scheme is a multi-paradigm Lisp language, suitable for students, that makes strong use of functional concepts, and is relatively easy to learn (if you ignore the parentheses).
  • Clojure is another Lisp dialect. Some claim that it is even more modern and more functional than Scheme, with the advantage of running on the JVM. Personally I have not had the opportunity to learn Clojure further, but everything indicates that it is one of the more interesting languages to use today.
  • Scala is a language that I have professional experience, and that I can therefore speak better. At first glance it looks a lot like Java and Ruby, but by "inside" you can see that it was inspired a lot by Haskell. It is not fully functional, and is far from pure, but it is usually a good option for those who have experience in OOP languages like Java or C#. Libraries like the Lift (a Web framework) they also allow us to see, in practice, the advantages (and disadvantages) of functional style in areas typically dominated by traditional object orientation.
  • F # is, like Scala, another example of functional hybrid X objects. But F # "gets heavier" on the functional side, because it is a superset of Ocaml. It can also help in obtaining the "prerequisites" needed to learn Haskell (for example, the computation expressions of F# are monads, which in turn are very important to understand Haskell).

Other than these, you can use the main functional concept, first-class functions, in Ruby, Python and even in C# (3.0+) and Java (8.0+).

 12
Author: rsenna, 2014-08-24 22:15:09

I will not guarantee that I will answer your question with the excellence you expect.

First of all it never hurts to learn something new. To tell the truth it is even healthy to get out of the comfort zone a little. Getting out of it will always make us come back better than when we left.

It is one thing to apply this kind of paradigm in the market that is totally dominated by OOP, Web and Mobile. This can be a barrier, as most IT companies work with beans with rice (CRUD, small screen that pulls data from here, prints report from there...).

A very positive point of functional programming is the possibility of working with lists and performing absurd calculations by writing very little code. We do a lot by writing very little, I think that's one of the strongest points of this type of language.

Finally, we are in the age of integration. There are no barriers that will prevent you from integrating Haskell with another market language. Today for everything there is a way. :)

I hope I helped.

 7
Author: Edgar Muniz Berlinck, 2014-04-18 03:24:43

I recommend this Haskell tutorial in English, which the author called "Learning Haskell will be a great good for you". Teaches Haskell in a very didactic and fluent, light, friendly way. It's worth checking out.

Edition:

I just discovered that this tutorial is the English version of Learn you a Haskell for Great Good, which has already been quoted here in this comment.

 0
Author: Sony Santos, 2017-04-13 12:59:31