1

I'm taking my first class on functional programming. I sorta just jumped in with no idea what I was getting into. But already I have learned that functional programming languages are distinct from another category of languages known as imperative languages. I gather C and C++ are examples of the latter.

Learning a programming language obviously takes time and effort. So far, I haven't seen anything indicating that imperative languages have any limitations compared to functional ones.

My Question:

Do functional programming languages offer benefits that make them widely useful to all programmers?

Stan Shunpike
  • 2,165
  • 3
  • 20
  • 32

2 Answers2

4

It's actually the other way around. Functional languages are the ones with the limitation. And this limitation is a tremendously valuable feature. When you work in a purely functional language (or a purely functional subset of a language), you lose the ability for programs to have side-effects, but this gives you a powerful ability at a higher level of abstraction: purely functional programs are easier to reason about and easier to combine into larger systems. These benefits broadly fall into two categories (that are really two sides of the same coin):

  • Modularity - the property that your software can be separated into modules and recombined in ways that their design does not necessarily anticipate.
  • Compositionality - the property that software components can be combined according to predictable laws. Thus, if you understand your components, and understand the laws of composition, you understand your whole software system.

Additionally, if your language is a strongly typed one (such as Haskell), the compiler can statically guarantee that you're not introducing side-effects and losing these abilities.

If your language is a total functional programming language, then your language can do even less. Namely, such languages do not support general recursion or infinite looping. But you gain the ability to have the compiler guarantee that your program cannot crash or hang. Imperative languages with general recursion or looping can never give you this guarantee.

Apocalisp
  • 34,834
  • 8
  • 106
  • 155
0

Yes. In purely functional languages, you can say:

f (print 5)

and the argument to f is the literal statement print 5 (compiled to executable code of course --- not a string!). In imperative languages, the same expression would execute print 5 --- printing the number --- and then pass the 'result' to f. So this function:

do_three_times a = do
    a
    a
    a

which is a perfectly sensible Haskell function, can be called like this:

do_three_times (print 5)

to print the number 5 three times; but in an imperative language it's not possible to write an exactly analogous function. (The nearest you can get is a dance with zero-argument functions, which is annoying enough it discourages people using imperative languages from using it).

The net effect of this is that, in imperative languages, your structured loop constructs have to be built into the language, so you have a small, fixed list of them; while in purely functional languages structured loop constructs are just library functions, so there are many more of them and you have much more freedom to pick the one that's suitable for your program.

Jonathan Cast
  • 4,569
  • 19
  • 34