LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Functional Programming? (https://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.

raskin 07-12-2010 12:23 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4031052)
H_TeXMeX_H:

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).

Sorry to disappoint you, but even Python has closures. But as we are discussing functional programming languages here, Common Lisp, Scheme, Clojure, OCaml, Haskell, JavaScript are also fair game.

By the way, delegates (bound object methods passed as functions) were present in Object Pascal and so semantically closures were even there (it was clumsy for some uses, because it was apparently optimized for a fixed use case)

Sergei Steshenko 07-12-2010 12:27 PM

Quote:

Originally Posted by raskin (Post 4031065)
Sorry to disappoint you, but even Python has closures. ...


Half way: http://en.wikipedia.org/wiki/Closure...ence%29#Python .

Rather, used to be half-way until Python 3.1 - I have already written in a different forum that after more than 10 years Python finally catches up with Perl.

On my Suse-11.1 Python version is 2.6.

H_TeXMeX_H 07-12-2010 12:29 PM

I can't write that in bash, bash arrays are no good, maybe in python or C. But, I don't think I would even need such a program as you wrote.

raskin 07-12-2010 12:31 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4031072)
Rather, used to be half-way until Python 3.1 - I have already written in a different forum that after more than 10 years Python finally catches up with Perl.

I said "even Python" for a reason. And 10 years is a great result - those catching up with Common Lisp, Haskell or SmallTalk tend to spend much more.

Sergei Steshenko 07-12-2010 12:33 PM

Well, but WRT anonymity Python is still behind Perl:

http://en.wikipedia.org/wiki/Anonymous_function#Python :

Quote:

Python supports simple anonymous functions through the lambda form. The executable body of the lambda must be an expression and can't be a statement, which is a restriction that limits its utility. The value returned by the lambda is the value of the contained expression. Lambda forms can be used anywhere ordinary functions can, however these restrictions make it a very limited version of a normal function.
.

Sergei Steshenko 07-12-2010 12:37 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 4031074)
I can't write that in bash, bash arrays are no good, maybe in python or C. But, I don't think I would even need such a program as you wrote.

So, write in "C" something similar, for starters without closures, and we'll discuss how readable it is and how it does (not) pollute global name space.

What I wrote is a rudimentary object - a set of encapsulated and possibly related data and functions.

MTK358 07-12-2010 01:55 PM

I wanted to say something here:

When reading about Haskell, I found that it said all variables were unchangable once they were created, and I understood that this was part of all functional languages. Is this true?

Sergei Steshenko 07-12-2010 02:06 PM

Quote:

Originally Posted by MTK358 (Post 4031137)
I wanted to say something here:

When reading about Haskell, I found that it said all variables were unchangable once they were created, and I understood that this was part of all functional languages. Is this true?

In Haskell variables are immutable; functional language treats functions as first class objects, i.e. functions can be passed as arguments and returned from other functions - mutability is a separate issue.

Immutability allows much better optimizations and formal correctness proofs.

OCaml, for example, allows to explicitly declare a variable or a field mutable, but recommends to do this as seldom as possible and when really needed. The tutorial has examples/explanations on this.


All times are GMT -5. The time now is 06:47 PM.