Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why purely functional languages instead of "impure" functional languages?

What's the advantage, for someone who is not a theoretical computer scientist, of learning a purely functional language like Haskell or Erlang instead of an "impure" language with a strong functional component, like Python or version 2 of the D programming language? My arguments are as follows:

  1. No paradigm is perfect. Languages that are single-paradigm, regardless of what paradigm that is, are very restrictive.
  2. Python or D will ease you into functional programming while you get real work done. Haskell or Erlang will force you to learn functional style all at once before you can actually do anything of interest in them.

Edit: By "impure" functional languages, what I really mean is strongly multiparadigm languages w/ a functional flavor, not strongly functional languages with some non-functional abilities.

like image 577
dsimcha Avatar asked Nov 24 '09 22:11

dsimcha


People also ask

Why might we want to write an application using pure functions instead of impure functions?

Pure functions are easier to debug In the small scale, pure functions are also easier to debug than their impure counterparts. Because the output of a pure function depends only on the function's input parameters and your algorithm, you don't need to look outside the function's scope to debug it.

Why are pure functions better?

Because they don't modify the global state, they are faster than regular functions. They also use less memory, which is important for applications with large data sets. Finally, pure functions are simple and easy to understand. You don't need to know anything about the internals of a function to use it correctly.

What is the difference between a pure and impure functional programming language?

Your function is pure if it does not contain any external code. Otherwise, it is impure if it includes one or more side effects.

How is a pure functional language distinguished from other functional languages?

Strictly speaking, a pure functional language is a functional language (i.e. a language where functions are first-class values) where expressions have no side effects.


4 Answers

It all depends on what are trying to achieve. If your goal to write production code in a functional language - a 'pure' functional language can make it more difficult.

But if you are trying to learn new concepts, 'pure' language gives you the benefit of guarding where you are sliding off the mark of functional concepts. Once you have clear understanding of differences you can go to a mixed environments - like F#, but before that it is all too easy to slip to the OOP way of doing things and because of this miss the advantages of functional approach.

In some other thread I offered an opinion that a better way of learning F# is to start with let us say Haskell (and was voted down for this), but if you learn F# to do OOP than what's the point - stay with C#

like image 73
mfeingold Avatar answered Oct 19 '22 22:10

mfeingold


You almost answered your own question:

Haskell or Erlang will force you to learn functional style all at once before you can actually do anything of interest in them.

Actually, depending on what you mean by 'of interest', you can be productive in Haskell in a week.

The main reason for anyone to learn Haskell (language theorists already know it, and other kinds of theorists are too busy proving theorems to be bothered with programming) is that learning Haskell will change the way you think about programming. Especially the type system, list comprehensions (stolen for Python—the highest form of praise), and programming with pattern matching. You will be able to use many of your new thoughts in all the programming you do. And Haskell will force you to think new thoughts in a way that other langauges won't.

Erlang is an honorable language but has nothing comparable to Haskell's type system.


If you like Paul Graham you can read more about this line of reasoning in his essay Beating the Averages, especially the part about the "Blub Paradox". Just substitute Haskell for Lisp :-)

like image 33
Norman Ramsey Avatar answered Oct 19 '22 23:10

Norman Ramsey


You learn very little if your "new" language is just a slight permutation of what you already know. It's like asking, "Why learn Chinese when I can just get a dialect coach to teach me to speak with a Scottish brogue?" I guess it's fine if you enjoy speaking with a brogue, but you're not really expanding your expertise very much.

Learning a functional language teaches you a new way of looking at things. Impure or mixed-paradigm languages like OCaml are good as well, but it can be tempting to use the impure elements as a crutch to avoid having to look at the problem in a new way. No, functional languages are not a magic bullet, but they do have a lot of interesting benefits, and you're robbing yourself of those benefits if you learn a language that has a "functional components" but doesn't really work like a real functional language.

For example, in a pure functional language like Haskell, state is very carefully isolated from the rest of your program. This makes all sorts of optimizations trivial that are very hard in other languages. For example, state is the enemy of parallel processing. In Haskell, you can just look at a function's type and be 100% confident that it won't create any side effects.

This way of thinking in functions that work with immutable data structures something that a pure functional language can teach you. I'm not saying pure functional languages are "the best," but they have their benefits. It's another tool in your belt. And you won't get that tool by sticking with what's familiar.

like image 32
Chuck Avatar answered Oct 19 '22 22:10

Chuck


Is Erlang purely functional? I honestly thought it wasn't.

I'm not an expert on this, so take my words with a grain of salt.

Side-effects prevent (or make more complicated) lots of optimizations, including parallelism. So, in theory, going pure functional should mean better optimization for faster code, although I'm not sure this is true in practice. Even if it does not, it might someday be... it will be fun if a purely functional language comes along which makes using up all the cores easy peasy.

Additionally, in a way, programming without side-effects makes for easier to understand programs.

like image 41
alex Avatar answered Oct 19 '22 23:10

alex