PDA

View Full Version : A question about functional languages.


Tarkus
11-16-2002, 07:55 PM
Just a question... How are functional languages useful and what advantages do they have over a procedural language?

I've been wondering this for some time...

sans-hubris
11-17-2002, 01:47 AM
They're great for writing compilers or interpreters.

jemfinch
11-17-2002, 07:21 PM
They're great for writing anything.

Think of functional programming like you think of Object Oriented programming. It's not defined by the language used, but by the way in which the language is used. Some languages, of course, facilitate functional programming better than others, just like C++ facilitates Object Oriented programming more than C does. But just as you can write object-oriented code in C, you can write functional code in any language.

With that said, the functional paradigm generally helps by keeping bugs out of code in the first place. By removing state from the list of things a programmer has to keep track of, functional programming lets the programmer spend more brain resources on more important things (algorithms, data structures, etc.)

The functional programming paradigm also encourages more generic, higher-order programming, which facilitates reuse. Instead of writing a singly-linked list function "length", you write a much more generic function, "foldl", and then implement length in terms of foldl:


datatype 'a list = CONS of 'a * 'a list | NIL

fun foldl f acc [] = acc
| foldl f acc (hd :: tl) = foldl f (f (hd, acc)) tl

val length = foldl (fn (_, acc) => acc + 1) 0


If you didn't understand that code example, it's alright :) The main point is that foldl can be used to define a number of functions, even ones that weren't originally conceived when the datatype list was defined. The code example does, however, show two very important abilities that are almost essential for a language to truly facilitate functional programming: the ability to use functions as first-class objects (shown in the use of a function argument to foldl) and the ability to define functions anonymously (that is, to build functions from runtime values, shown by the use of "(fn (_, acc) => acc + 1)" in the definition of length).

The main advantages of functional programming over procedural programming are, in my opinion, fewer bugs and more code reuse.

Jeremy

GnuVince
11-17-2002, 09:26 PM
Jeremy summed it pretty well. There's another thing I like about functional programming: the code is more elegant. Elegant code is simple and concise code, and I love that; especially if other people are going to be looking at it.

file13
11-18-2002, 03:51 PM
another thing that's nice about functional programming is that you get immediate feedback. with a procedure you have internal side-effects which are not immediatly apparent. but when you program everything to return something you get immediate feedback. combine this with the fact that most (all?) of your functional languages have interactive environments and you end up getting instant feedback. you can test it out right after it's written without having to write a driver--like in C/C++--and without the compiler/link steps. this speeds up development tremendiously! but also note that in many non-functional languages, functional programming can be a hassle because most non functional langauges only allow you to return one value, don't treat them as "first-class" values, don't allow optional values to be passed, and don't optimize recursion. in fact i find OOP less useful with a functional language because it's so easy to toss about functions. rather then write a class heirerarcy with specific methods, you could write one general function and pass other functions for the "class" specific stuff. this also avoids the OOP problem of "structured spaghetti code."

GnuVince
11-18-2002, 05:46 PM
Hey file13! Been a long time!

You're right, I think that right now, the toplevel-loop is an indispensible tool. It helps code better and faster IMO. And when learning a new language, testing new stuff is _so_ much easier

file13
11-18-2002, 06:59 PM
i've been here. just been a busy boy.... ;)

jemfinch
11-18-2002, 07:23 PM
But then, a REPL ("toplevel loop" in O'Caml-scented terms) is available for nearly every language out there.

Jeremy

GnuVince
11-18-2002, 07:42 PM
jemfinch: Yes, and that is a very good thing in my opinion. At the cegep where I go, they teach RPG/400, which has no REPL. I think if they used a language that could be used interactively, the students would be more interested, and would be better.

Tarkus
11-18-2002, 11:23 PM
Thanks, cleared up my curiosity quite nicely...

Flangazor
09-19-2003, 09:32 AM
Amusingly, one of the most popular areas of C++ design interest nowadays is in functors. They are pretty much closures and have been around for decades in functional languages!