LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Functional Programming? (http://www.linuxquestions.org/questions/programming-9/functional-programming-819310/)

MTK358 07-11-2010 08:06 PM

Functional Programming?
 
I wanted to continue this because I found it interesting:

http://www.linuxquestions.org/questi...ml#post4021617

Still, what is the best way to learn functional programming, or at least get the concepts behind it?

And is LISP functional? It looked quite imperative to me.

raskin 07-12-2010 03:05 AM

Common Lisp is whatever you make it. You can write functional-style code in it or imperative code with it. Scheme (also a member of Lisp language family, but slightly different) is more inclined to be functional, but even it allows writing imperative code if you really want it. Haskell is a language that claims to be purely functional. Unfortunately, there is so much syntax sugar in Haskell that some people manage to write imperative-style Haskell tutorials despite all that...

Some people recommend "Structure and Interpretation of Computer Programs" to learn fucntional programming and Scheme. Of course there are many tutorials to learn Haskell or Scheme out there (I recommend Scheme). I cannot advise anything specific for learning Scheme because I was taught some fuctional programming in high school using a barebones toy language, and learned Scheme and Common Lisp by reading the specifications..

It may be that learning APL could be a nice start for learning functional programming, too..

H_TeXMeX_H 07-12-2010 03:24 AM

I've tried common lisp at one point. Well, it's interesting as a concept, but I don't really like all the parentheses. Sure, I guess a good IDE will handle them, but then I asked myself what good will common lisp do me. I've never seen any programs written in lisp, or maybe 1 or 2. I already learned C/C++, much of which I've forgotten, and had to re-learn, and I don't even use C that often, and C++ almost never. Bash does everything I need in much less time than it would take to write a C program. I would only write a C program if there is none already available that can do the same thing, or if better performance is needed somewhere.

raskin 07-12-2010 03:30 AM

H_TeXMeX_H, so you just do not need to do any programming. I know bash scripting quite well (up to writing a small site as a CGI in POSIX shell), but it is simply not enough for many things.. And so I use Common Lisp... Its usefulness is only visible when Pascal stops being enough.

Sergei Steshenko 07-12-2010 06:17 AM

Quote:

Originally Posted by MTK358 (Post 4030342)
I wanted to continue this because I found it interesting:

http://www.linuxquestions.org/questi...ml#post4021617

Still, what is the best way to learn functional programming, or at least get the concepts behind it? ...


http://www.ocaml-tutorial.org/functional_programming

MTK358 07-12-2010 06:18 AM

<didn't see above post>

Sergei Steshenko 07-12-2010 08:06 AM

Quote:

Originally Posted by MTK358 (Post 4030726)
<didn't see above post>

http://perldoc.perl.org/perlfaq7.htm...s-a-closure%3f

RockDoctor 07-12-2010 08:06 AM

Quote:

Originally Posted by raskin (Post 4030569)
It may be that learning APL could be a nice start for learning functional programming, too..

APL makes obfuscated Perl look eminently readable ;)

bgoodr 07-12-2010 10:13 AM

Quote:

Originally Posted by raskin (Post 4030569)
Some people recommend "Structure and Interpretation of Computer Programs" to learn functional programming and Scheme. Of course there are many tutorials to learn Haskell or Scheme out there (I recommend Scheme).

I second that. SICP I believe is still on the web for free.

From personal experience, I highly recommend that folks try to learn more languages than they actually use in day-to-day work. Try to learn them even if it makes your brain hurt (not all discomfort is bad). As a result, your thought process with working with the current set of "popular" languages will benefit greatly from it. For me, the two A and B languages were Scheme and C++ (I use C++ heavily in my daily work, only because it pays the bills in my chosen corner world where I chose to stay).

The reason Scheme (and Common Lisp to some degree) have stood the test of time, is due to their macro systems, not due to their embodiment of one particular programming fad. Want to program in a new paradigm? Then search for a macro library that provides that feature, or roll your own after that search is exhausted.

As your proficiency with several languages grows, you will find the rough edges of those languages. That applies to any languages. But, the advantage of the CL and Scheme branch of languages is that you can break out of those limitations by careful use of macro constructs. This is really the ability to extend the language.

As an example of the "A versus B" case, I run into this situation quite frequently in my own work: C++ (and its C predecessor) lack a good macro system. As programs get larger and larger, they need to be refactored to use constructs that operate at a higher level of abstraction than you can obtain by functional decomposition, object-oriented decomposition, or by generic programming. I cannot tell you how many times I've coded the same construct in C++ over and over again simply because there was no way to express exactly what I meant without resorting to the crippled C preprocessor (Read the GLIBC library source code, and you quickly discover that a much better macro system than what the C preprocessor provides, is in great need!).

Notice above that I am comparing Scheme's macro system to the C preprocessor, not comparing Scheme to C++.

Quote:

Originally Posted by H_TeXMeX_H (Post 4030582)
I've tried common lisp at one point. Well, it's interesting as a concept, but I don't really like all the parentheses. Sure, I guess a good IDE will handle them, but then I asked myself what good will common lisp do me.

Choosing languages so as to avoid learning surrounding tools (such as sophisticated editors, and IDE's), is a mistake. You will find that languages that continue to evolve, such as C++, see the introduction of more complex syntax, and that inevitably leads to more complex constructs using more punctuation than just parentheses. Again, C++ has added angle brackets for C++ template constructs, and it already requires you to use curly braces and parenthesis. Compare that with other languages that wanted to "bolt on" generic programming constructs. You will, over time, beg for a sophisticated and customizable IDE to navigate all of the punctuation and constructs of multiple languages in a production software environment.

bg

raskin 07-12-2010 10:52 AM

Quote:

Originally Posted by RockDoctor (Post 4030850)
APL makes obfuscated Perl look eminently readable ;)

Actually, no. APL has a simple and strict grammar - just an unusual one. Perl has a ball of duct tape instead of grammar, and I am not sure it is even decidable.

And learning APL will make you learn something about pure functional programming (the things you caould have missed even with Scheme) and highly expressive array processing constructs - while Perl will only teach you to cut corners.

H_TeXMeX_H 07-12-2010 11:37 AM

I agree that Perl is messy language, I wouldn't recommend it as a first language, it doesn't teach you how to program, only how to hack or yeah "duct tape" things together. Of course, you can still do stuff with it, but I decided not to learn it because of these aspects. I'll stick with C and some bash, and a bit of python.

The only functional language that has stood out for me is OCaml, I think you might wanna try it, like Sergei suggests. They boast performance nearing that of C, and it's like an all-in-one language. You can compile to byte code or fully compile. And it is FLOSS.

Sergei Steshenko 07-12-2010 11:55 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 4031025)
I agree that Perl is messy language, I wouldn't recommend it as a first language, it doesn't teach you how to program, only how to hack or yeah "duct tape" things together. ...

A typical rant of non-knower. Write this (a legal and meaningful Perl code):

Code:

sergei@amdam2:~/junk> cat -n fancy_data_structure.prl
    1  my $some_coeff = 1.5;
    2
    3  my $hash_ref =
    4    {
    5    yet_another_hash =>
    6      {
    7      one => 1,
    8      two => 2
    9      },
    10
    11    array_of_hashes =>
    12      [
    13        {
    14        name => 'John',
    15        address => '1 Mary st'
    16        },
    17
    18        {
    19        name => 'Paul',
    20        address => '5 James st'
    21        }
    22      ],
    23
    24    a_method_with_two_closures =>
    25      do{
    26        my $another_coeff = 3.0;
    27
    28        sub # anonymous subroutine
    29          {
    30          my ($x) = @_;
    31
    32          $some_coeff * $another_coeff * $x; # the returned value
    33          }
    34        }
    35    };
sergei@amdam2:~/junk>

in shell and then we'll talk.

MTK358 07-12-2010 12:02 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4031039)
A typical rant of non-knower. Write this (a legal and meaningful Perl code):

...

in shell and then we'll talk.

I thought he meant Perl vs C, Python, Java, etc., not Perl vs. Bash.

Sergei Steshenko 07-12-2010 12:08 PM

Quote:

Originally Posted by MTK358 (Post 4031045)
I thought he meant Perl vs C, Python, Java, etc., not Perl vs. Bash.


H_TeXMeX_H:
Quote:

Bash does everything I need in much less time than it would take to write a C program.
Anyway, I'm impatiently waiting to see how H_TeXMeX_H will implement the suggested code snippet in C, Python, Java (well, the very very latest Java is supposed to have closures).

raskin 07-12-2010 12:15 PM

Quote:

Write this (a legal and meaningful Perl code):
First, it is possible to write good readable code in Perl - and you have given a fine example. It just happens too rarely.

Second, what you have shown is a thing that can be done in any advanced shell, although in a clumsy way including evals. But if you want such things from the beginning, you could just pick a suitable language. You have given a fragment where Perl isn't even doing anything that is inconvenient in Python.

Now, Perl is a language with no overall design, and Python has only imitation of overall design principles. As the original question was about picking a language for learning concepts, language with an overall design usually gives you more.


All times are GMT -5. The time now is 03:13 PM.