LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Learning functional programming (http://www.linuxquestions.org/questions/programming-9/learning-functional-programming-714373/)

Robhogg 03-25-2009 12:53 PM

Learning functional programming
 
I am rather fascinated by what little I know of functional programming, and would like to learn more, when I have time. Been doing a bit of shopping around, and not sure which direction to head in.

I currently am fairly competent in PHP, Java and Perl. My aim with FP would be to learn a new way of approaching or thinking about programming.
  • The Haskell books do not tend to get great review on Amazon, but I did find the chapter in Beautiful Code enticing
  • Programming Erlang by Joe Armstrong does get a good review, but only one
  • I understand Lisp is the "grand-daddy" of them all, and Practical Common Lisp does get good reviews
  • Or I could stick with a language I already know something about and get Higher Order Perl

So any advice and book recommendations?

Thanks,
Rob

indienick 03-25-2009 01:10 PM

Lisp, Lisp, Lisp!!!

It's a blast. :)

My biggest selling point on Lisp is its syntax; some people (like myself) love it, other people hate it (saying things like "too many parentheses").

Practical Common Lisp is a swell book - it got me started on the language - but I highly recommend, also, getting a copy of ANSI Common Lisp (it makes for a killer reference book for standard functions), and see if you can get a copy of Common Lisp: A Gentle Introduction to Symbolic Computation (it provides a wonderful, thorough, easy-to-follow introduction on how Lisp works - thus, explaining its syntax).

Robhogg 03-25-2009 01:18 PM

Quote:

Originally Posted by indienick (Post 3487463)
Lisp, Lisp, Lisp!!!

You sound like a friend of mine when Ruby is mentioned ;-).

So if I wanted only one book to be going on with (at the moment, anyway), is Practical Common Lisp the one to start with?

indienick 03-25-2009 01:20 PM

I would most certainly say so!

Also, for what it's worth, Common Lisp: A Gentle Introduction to Symbolic Computation is available for free as a PDF - just take it to a Kinkos (or "FedEx Express" as they're called, now) and get them to print it, and bind it, for you.

H_TeXMeX_H 03-25-2009 01:29 PM

But, I thought functional programming was superseded by OOP ... right ?

If you like functional programming, go for C, it'll be useful and functional programming :) (but may be a bit difficult, but if you know other languages already it shouldn't be too hard)

taylor_venable 03-25-2009 01:37 PM

Haskell is a good language but I find that there are many things that I do not yet understand about it. Real World Haskell is supposed to be good but I haven't read it yet. One of my biggest problems with Haskell though is that the main implementation that everybody uses doesn't run on many platforms. Also it's about the opposite of Lispy languages in that it has a lot of syntax, people love to make random operators and as a result Haskell source can be very dense and hard to read.

Common Lisp is a great language with many good implementations. It's a bit messy for my tastes in some respects (e.g. functions that take a thousand optional parameters) but it's designed to be complete, not necessarily simple. Paul Graham's books ANSI Common Lisp and On Lisp are great.

Scheme is another great one, and IMHO the best for a beginner, though that does not by any means imply that it's limited. I've been reading R. Kent Dybvig's The Scheme Programming Language which is excellent. By chapter three he covers how everything in Scheme can be implemented with a few core forms. It really changes the way you think about programming to realize that looping and variable binding can be done with lambda forms and function application. You should also read the Lambda the Ultimate papers as well if you go this route. (See also: http://www.ccs.neu.edu/home/dorai/t-...-y-scheme.html)

There are also strict (i.e. non-lazy) strongly statically typed functional languages like SML and OCaml which are both worth learning as they give you a slightly different angle on things. Erlang is functional but adds some neat elements from Prolog to the mix.

Given infinite time and energy, I would learn Scheme completely (that won't take long). Then touch Common Lisp to understand its differences with Scheme. Next learn Standard ML and pick up Haskell to the point that you understand its additions to the type system and use of laziness. Hit Erlang if you want; pay attention to its concurrency support, error-handling systems, and pattern resolution. Then you'll be ready to use functional features in day-to-day languages (Higher-Order Perl is a very good book) and understand when / why they're more limited than comparable features in "real" functional languages.

Sergei Steshenko 03-25-2009 01:39 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3487486)
But, I thought functional programming was superseded by OOP ... right ?

If you like functional programming, go for C, it'll be useful and functional programming :) (but may be a bit difficult, but if you know other languages already it shouldn't be too hard)

Wrong. OOP, AFAIK, is easily derived from functional.

Regarding "C" - GNU dialect at least allows function definitions inside functions - very typical for functional programming.

...

"Perl is damn good functional language" (or something like that) - from OCaml tutorial.

Try OCaml and Haskell - Perl 6 was heavily influenced by Haskell.

The above two are strictly typed - unlike Perl 5.

3vra 03-25-2009 02:16 PM

I would recommend Haskell the craft of functional programming.I am a first year computer science student and I have tried a few other functional programming languages before.The beauty of Haskell is that it introduces functional programming using it's own built data types and functions and when you get an understanding of the paradigm then the fun stuff starts.You get to "go under the bonnet" of these data types and functions to see how they have been written and how easily you see how they were created when you get to understand how the work.The approach is gentle and you wont get lost if you follow the book page by page.

Robhogg 03-25-2009 03:49 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3487486)
If you like functional programming, go for C...

C is generally considered to be an imperative programming language. Functional programming is something a lot weirder. Although I daresay, as Sergei suggests, you could write functional stuff in C. One of the other fascinating essays in Beautiful Code is on theLinux kernel drivers model - halfway through reading it I thought "hey - they're doing object oriented programming. In C!"

The difference is (as I understand it), in imperative languages you write a series of instructions to get from state A to state B, whereas in functional language you write a function (akin to a mathematical function) that describes the relationship between state A and state B, and let it get on with it.

@Taylor_Venable:
Scheme is a language that I've only barely heard of (although I could say the same for Erlang). Sounds interesting, though. Thanks - I'll definitely check out the link.

What attracts me most to Lisp is Greenspun's Tenth Rule of Programming
Quote:

Any sufficiently complicated C or Fortran program contains an ad hoc informally-specified bug-ridden slow implementation of half of Common Lisp.
:D

@3vra:
I'd been a bit put off of that book by the very mixed reviews on Amazon, but I will give it another look.

Thanks,
Rob

Sergei Steshenko 03-25-2009 04:15 PM

Quote:

Originally Posted by 3vra (Post 3487524)
I would recommend Haskell the craft of functional programming.I am a first year computer science student and I have tried a few other functional programming languages before.The beauty of Haskell is that it introduces functional programming using it's own built data types and functions and when you get an understanding of the paradigm then the fun stuff starts.You get to "go under the bonnet" of these data types and functions to see how they have been written and how easily you see how they were created when you get to understand how the work.The approach is gentle and you wont get lost if you follow the book page by page.

Haskell looks really clean and elegant, however, it's a pure functional language. And sometimes this makes life difficult.

Opposed to Haskell OCaml has two clearly defined parts: functional and imperative. This is good IMO - when pure functional approach gets in the way of solving the problem one can switch to imperative. An example they give is GUI stuff.

It appears that Haskell and OCaml communities are friendly towards each other.

I think OCaml is more practical; it has interesting "dialects" - see JOcaml, for example. In this case dialect is a good thing.

taylor_venable 03-25-2009 05:04 PM

Robhogg: In the beginning there was LISP; then came Scheme and Common Lisp. Both Scheme and CL are descendants of the original ideas in LISP, they've just gone down different paths of evolution. In my mind, saying "LISP" or "Lisp" is more ideological, and for implementation you've then got the choice of Scheme or Common Lisp. I recommend Scheme first because those original ideas are more simplified, generalised, and laid bare in Scheme than CL. Learning CL after Scheme can show you new or different ways in which those core ideas were extended to fit a different kind of goal in the language.

indienick 03-25-2009 09:30 PM

I would just like to tack on to the end of taylor_venable's post, that in Scheme being a much cleaner (argumentatively "purer") Lisp dialect, from what I have seen it places a heavier emphasis on the use of Lambda functions in places where Common Lisp would already have a standardized function.

Telemachos 03-25-2009 10:53 PM

If you're familiar with Perl and curious about functional style programming and higher order programming (passing functions as values to functions), then I cannot recommend Higher Order Perl strongly enough. It's available online free, and well worth buying if you prefer dead tree books. (I still prefer paper for books I really carry about.)

I'm also surprised that nobody's mentioned Structure and Interpretation of Computer Programs which is an all-around classic.

ta0kira 03-25-2009 11:18 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3487486)
But, I thought functional programming was superseded by OOP ... right ?

If you like functional programming, go for C, it'll be useful and functional programming :) (but may be a bit difficult, but if you know other languages already it shouldn't be too hard)

C is procedural with minimal OOness. Functional programming is also OO in one way or another.
Kevin Barry

edit:
Unless you mean "you can write programs that function in C," which I wouldn't argue with...

3vra 03-26-2009 11:48 AM

@Robhogg
I think you need to be a bit more specific regarding your intentions.Do you want to go deep into functional programming or do you want to or you just want to be knowledgeable.

All the languages mentioned are really good languages and the all can help you to understand functional programming but there are programming languages that are designated to strictly functional programming.Therefore depending on what you want we can make better recommmendations.


All times are GMT -5. The time now is 04:42 PM.