LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2010, 08:06 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Thumbs up 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.
 
Old 07-12-2010, 03:05 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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..
 
Old 07-12-2010, 03:24 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 07-12-2010, 03:30 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 07-12-2010, 06:17 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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
 
Old 07-12-2010, 06:18 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
<didn't see above post>
 
Old 07-12-2010, 08:06 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
<didn't see above post>
http://perldoc.perl.org/perlfaq7.htm...s-a-closure%3f
 
Old 07-12-2010, 08:06 AM   #8
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Manjaro
Posts: 1,791

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
Quote:
Originally Posted by raskin View Post
It may be that learning APL could be a nice start for learning functional programming, too..
APL makes obfuscated Perl look eminently readable
 
Old 07-12-2010, 10:13 AM   #9
bgoodr
Member
 
Registered: Dec 2006
Location: Oregon
Distribution: RHEL[567] x86_64, Ubuntu 17.10 x86_64
Posts: 221

Rep: Reputation: 36
Quote:
Originally Posted by raskin View Post
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 View Post
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
 
Old 07-12-2010, 10:52 AM   #10
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Quote:
Originally Posted by RockDoctor View Post
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.
 
Old 07-12-2010, 11:37 AM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 07-12-2010, 11:55 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by H_TeXMeX_H View Post
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.
 
Old 07-12-2010, 12:02 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 07-12-2010, 12:08 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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).
 
Old 07-12-2010, 12:15 PM   #15
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Book Excerpt: Chapter 6: Functions and Functional Programming LXer Syndicated Linux News 0 10-10-2009 05:11 AM
Learning functional programming Robhogg Programming 17 03-27-2009 08:58 AM
functional programming in C++ synss Programming 2 08-03-2007 01:33 AM
Functional Programming - Discussion taylor_venable Programming 6 07-30-2006 10:20 PM
LXer: Everything Your Professor Failed to Tell You About Functional Programming LXer Syndicated Linux News 0 01-31-2006 09:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration