Do you need a functional programming language to program functionally?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Do you need a functional programming language to program functionally?
Be patient with me, here, but I was getting started on learning Haskell through the intro guide at http://www.haskell.org/haskellwiki/Introduction. It has the big section about how two lines of functional code in Haskell equals two dozen lines of imperative code in C. The Haskell code was:
// To sort array a[] of size n: qsort(a,0,n-1)
void qsort(int a[], int lo, int hi)
{
int h, l, p, t;
if (lo < hi) {
l = lo;
h = hi;
p = a[hi];
do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h) {
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);
a[hi] = a[l];
a[l] = p;
qsort( a, lo, l-1 );
qsort( a, l+1, hi );
}
}
And that makes sense, of course, if you program it in C in an imperative manner. But couldn't you just program it in a functional manner, and create basically the equivalent code? I didn't want to do that in C, 'cause I didn't want to mess around with the issue of array sizes. But in Ruby, this works fine: (usage code included)
Code:
def qsort(arr)
if arr.length == 0
return []
else
return [qsort([ arr.reject {|x| x >= arr[0]} ].flatten), arr[0], qsort([ arr.reject {|x| x <= arr[0]} ].flatten)].flatten
end
end
arr = [5, 2, 8, 3]
new_arr = qsort(arr)
puts new_arr
I sure there are other reasons to use Haskell, of course, I'm just asking: If you use an "imperative" language to program "functionally" what's the big difference?
Last edited by tinyTux; 04-13-2011 at 04:16 PM.
Reason: typo
I suppose similar arguments were made about whether you could do "object oriented" programming in procedural languages like FORTRAN or Pascal.
Clearly, functional programming is easier in languages that specifically support it, like Erlang or Haskell.
Interestingly, Javascript (arguably the most-used language on the planet!) supports functional programming extremely well. So well, in fact, lots of "functional code" is probably out there without the developer even realizing he/she was DOING "functional programming!
So I guess my reply to your question is: "Whatever works !"
'Hope that answers your question ... at least partially.
I suppose similar arguments were made about whether you could do "object oriented" programming in procedural languages like FORTRAN or Pascal.
reminds me of that guy that could do object-oriented programming in assembly.
Reasons for learning a language or not is affinity of other developers with the language, learning curve and apart from that: most languages are good/best practice for a specific type of task. Some languages are extremely well in data-organization, whereas others are best for low-level system resources, others have a tendency to be good for general-purpose; and so every language has strengths and weaknesses.
I sure there are other reasons to use Haskell, of course, I'm just asking: If you use an "imperative" language to program "functionally" what's the big difference?
Not a lot - although there might be some pitfalls, especially with C. For example, one defining characteristic of programs written in a functional style is that their functions return new objects without changing the old ones. For example, in the following LISP code:
Code:
(reverse (mapcar #'double x))
The mapcar function takes a function (#'double) and a list (x) and returns a brand-new list (let's call it 'y'), and then the reverse function takes that list and returns another brand-new list (let's call it 'z'). Well, in a language where you want to do that, you need to have garbage collection - in C, the list 'y' would have been allocated on the heap, but we've lost all references to it, and so have a memory leak.
Off the top of my head, garbage collection is probably the most important thing to have in order to program in a functional style - so long as you have that, go for it.
(Edit: Other things that will help a lot are having functions as first-class objects and lexical closures, but they're not essential.)
Last edited by JohnGraham; 04-14-2011 at 03:46 AM.
In this context functional languages discipline your mind - they teach you to formulate problems and their solutions concisely, with strict mathematical flavor, omitting (when|where)ever possible implementation details, like, for example, traditional loops.
It's an instant "no" for me; this is so unreadable; nice that you saved a few keystrokes, but how do you go about debugging them?
I agree.
I tried to start learning Haskell at least 2 or 3 times, but gave up becasue even the basic, beginner tutorials had such unreadable gibberish for code that I just couldn't figure it out.
I tried to start learning Haskell at least 2 or 3 times, but gave up becasue even the basic, beginner tutorials had such unreadable gibberish for code that I just couldn't figure it out.
Have you tried to learn a foreign language without first learning the alphabet (if it's an alphabetic language) ?
I need to study into this more. Upon further reflection, I discovered there are some other issues here I don't understand well enough yet, such as dealing with state in a functional program (i.e., monads), as well as if and how the object-oriented paradigm can integrate with the functional one. These questions seem to be the most significant of any, and yet are the most puzzling.
I need to study into this more. Upon further reflection, I discovered there are some other issues here I don't understand well enough yet, such as dealing with state in a functional program (i.e., monads), as well as if and how the object-oriented paradigm can integrate with the functional one. These questions seem to be the most significant of any, and yet are the most puzzling.
OO is a formal consequence of functional. State in OO is well reflected by closures in functional. Virtual methods are essentially arguments which are functions.
I hate to eat my own hat, so to speak, but I spent some more time learning Haskell, as well as trying to apply functional programming principles in other languages, namely, Ruby and C++.
Starting off, C++ was very difficult for two reasons primarily: 1) No automatic memory management, and 2) one cannot return lists or arrays (only pointers to them). So you have a lot of mess to deal with deciding when to malloc and free stuff, which gets quite complicated in recursive functions. These two worked together to make creating pure and recursive functions quite impractical. Not to say one couldn't figure out workarounds, but it showed me that C++ was poorly fit for true functional programming.
Ruby is much better, but the wrenching difficulty is that Ruby does not support tail call optimization, meaning that you can't create tail-recursive functions without dealing with monster-like stack growth. Especially a problem if you want to create infinitely recursive functions to replace main loops and other such "imperative" constructs. Ruby 1.9 does have tail call optimization capability, but it is not enabled by default (because it would interfere with the compiled-in stack-trace functionality) and the capability is not actually guaranteed by the specification, but it is actually up to the implementation whether or not to include it.
Working with Haskell some more, it seems to me like Haskell provides a lot of unique language constucts that are better suited to the general functional paradigm; that is, wrapping programming into mathematical-like formulas, rather than simply listing sequences of commands. Programming in Haskell can actually be rather fun if you adopt the right mindset for it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.