Meaningless Drivel is fun!*
The moose likes Functional Programming and the fly likes need advice on learning functional programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Functional Programming
Bookmark "need advice on learning functional programming" Watch "need advice on learning functional programming" New topic
Author

need advice on learning functional programming

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

I need to broaden my horizons as a developer so this years topic is functional programming. I'm not even clear on the differences between functional, imperative, and procedural programming, so it seems like a good place to start.

I've started on the Python tutorials but I'd like to hear some opinions. What's the best way to learn functional programming? What are your favorite websites or books? Is Python a good choice or should I start with something else like Haskell or Scala?

btw, I have no experience with calculus, so I realize that I need to start with an understanding of lambda calculus. Pointers on this are welcome.

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

I'm starting down the same road, coming from a different background (mainly DB applications), and so far it's really interesting. So I'm no FP expert but here's some pointers to things I've found interesting, helpful or just plain fun. BTW I don't know anything about Lambda Calculus either, except that after looking at the Wikipedia article, it doesn't seem to have much to do with regular calculus (but I may be wrong here).

Anyway, first stop for FP might be Rich Hickey's entertaining introduction to why FP might be a good idea: Simple Made Easy.

Although I've got some knowledge of Python, which has some functional aspects e.g. lambdas and comprehensions, I've been looking mainly at more function-oriented languages.

Haskell is a pure functional language, and a good place to start with it might be Learn You A Haskell For Great Good, a great tutorial that is available as a free book online. The advantage of Haskell is that you have no choice but to program in a functional style, unlike Python or Scala, for example.

Of course, Lisp is another classic FP language, and the equally classic Structure and Interpretation of Computer Programs (SICP) lectures from MIT will introduce you to a lot of fundamental CS and FP concepts expressed via Scheme (a Lisp language). The SICP book is available online, although I bought a copy as it's good (if challenging) stuff. There are videos of the lectures online as well, although they're from the late 1980s I think, so the quality is a bit patchy in places.

On the JVM, the main functional languages are Clojure (a JVM implementation of Lisp from Rich Hickey), and Scala which is an interesting mix of FP and OOP and is being used for significant mainstream applications e.g. Twitter has been a prominent user of Scala, and the UK Guardian newspaper recently moved some of their systems to Scala.

I completed an excellent Coursera course on Functional Programming with Scala, presented by Martin Odersky, the guy who invented Scala. Not sure when it will be run again, but the course materials are available online at Coursera. Definitely one to watch out for.

Finally, people who know about JavaScript say it also has features that allow functional programming (e.g. closures), so that's another area that might be interesting to explore, although I haven't seen much in the way of learning materials for FP with JavaScript.

Good luck - and if you find any other useful stuff for those of us learning FP, be sure to share them here at JavaRanch.


No more Blub for me, thank you, Vicar.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

Since I posted that this morning I've learned the difference between "sorta-FP" languages like Python, and "pure-FP" languages like Haskell. So I might make the switch to Haskell.

Another decent looking link I found is Real World Haskell from O'Reilly. It's a free online book. Along with the links you posted, it looks like we have some good sources for Haskell and Scala.

I'll probably try several languages. I'm not looking to become an expert in FP or in any one of the FP languages, but I think it will give me another filter through which to view the world. It's a very different way of thinking compared to the procedural world that I'm comfortable with.

I also found this page very interesting, especially the part where it compares the Lambda Calculus computing model to the Turing model. Unfortunately, he lost me when he started using functions to describe integers. Maybe I'll come back to that page when I've learned a bit more.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Thanks for the extra links - very helpful.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

chris webster wrote:I completed an excellent Coursera course on Functional Programming with Scala, presented by Martin Odersky, the guy who invented Scala. Not sure when it will be run again, but the course materials are available online at Coursera. Definitely one to watch out for.

There's another Coursera course starting next week that appears to have a fair bit of functional programming in it: https://www.coursera.org/course/proglang

This is a comparative programming languages course, looking at the difference between different types of language with an emphasis on FP. It does so using three languages: ML, Racket (a functional language from the Lisp family), and Ruby. I've never used any of those, so I'm quite looking forward to it.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Matthew Brown wrote:
chris webster wrote:I completed an excellent Coursera course on Functional Programming with Scala, presented by Martin Odersky, the guy who invented Scala. Not sure when it will be run again, but the course materials are available online at Coursera. Definitely one to watch out for.

There's another Coursera course starting next week that appears to have a fair bit of functional programming in it: https://www.coursera.org/course/proglang

This is a comparative programming languages course, looking at the difference between different types of language with an emphasis on FP. It does so using three languages: ML, Racket (a functional language from the Lisp family), and Ruby. I've never used any of those, so I'm quite looking forward to it.

Yes, I signed up for that one as well, but it coincides with the MongoDB DBA course which I was thinking of doing, and I'm also trying to keep up with an ongoing OU stats course. Plus I need to find time to brush up my Java skills for job-hunting back in the real world. So many courses, so little time...
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

One more question for this thread...has anyone found a good discussion forum for Haskell? I can find a newfeed at haskell.org but I prefer a web based forum. There are a few forums that are either abandoned or full of spam, but I can't find anything remotely equivalent to Coderanch.

I'd like to have a place that's as friendly and helpful as Coderanch where I can ask specific questions, but I may be asking for way too much.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Jk Robbins wrote:One more question for this thread...has anyone found a good discussion forum for Haskell? I can find a newfeed at haskell.org but I prefer a web based forum. There are a few forums that are either abandoned or full of spam, but I can't find anything remotely equivalent to Coderanch. I'd like to have a place that's as friendly and helpful as Coderanch where I can ask specific questions, but I may be asking for way too much.

Haven't seen anything like that for Haskell, although I'm still working through "Learn You A Haskell" so it's early days for me yet. But I get the impression that Haskell practitioners are a a bit old school, so StackOverflow is about as Web 2.0 as they get.

If you're happy to try a different FP language, you could look at Clojure instead, which has several active Google Groups. There's a Google+ Clojure group as well, if you're into Google+.

As for me, I think I'm going to try the Coursera Programming Languages course that Matt mentioned, as it looks like a good way to find out more about FP in a variety of languages.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

I'm also working through "Learn You A Haskell", but I'm struggling with curried functions. I may try Clojure since curried functions are common to FP languages and are not Haskell specific.

I'm considering the Coursera course, but I just don't know if I can find enough hours in the week to devote to doing it properly. It sure looks like a great option. The more I think about it, the more I think it's the best way to go.

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

I said I was done with this thread, but I found a link that deserves to be posted somewhere. This article explains FP by using Java to write FP code. I found it very helpful. Now I wish the same writer would do some more articles. Just wanted to pass this along.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Jk Robbins wrote:I said I was done with this thread, but I found a link that deserves to be posted somewhere. This article explains FP by using Java to write FP code. I found it very helpful. Now I wish the same writer would do some more articles. Just wanted to pass this along.

Great article - I also read the same guy's post on LISP, which was very interesting too.

Incidentally, I've "shared" this thread with the new Functional Programming forum, so if you have any general FP questions, try the new forum first.

Thanks for sharing the article.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Neal Ford wrote a series of articles on FP which are available at IBM DeveloperWorks and cover some of the same ideas as he discussed in his "Functional Thinking" talk for Strange Loop 2011.
Gary Deer
Greenhorn

Joined: Aug 20, 2010
Posts: 26

Jk Robbins wrote:

I'm not even clear on the differences between functional, imperative, and procedural programming, so it seems like a good place to start.

I've started on the Python tutorials but I'd like to hear some opinions.

What's the best way to learn functional programming? What are your favorite websites or books? Is Python a good choice or should I start with something else like Haskell or Scala?

btw, I have no experience with calculus, so I realize that I need to start with an understanding of lambda calculus. Pointers on this are welcome.


The differences between functional, imperative and procedural programming:
Imperative: writing statements that change state.
example: start here,set x, do this to x, then that, get a file, write from file onto x, error if no file, x += "I'm a mutant", print x, done.
Procedural: imperative with subroutines. If state changes are localized or tied to a procedures then it's structured programming.
example: start here,set x, x.doThis(), x.thenThat(), x += openFile("mutants.txt").readLine().toString(), closeFile() , printf(x), return 0
Functional: avoid state and mutable data which means that functions don't have side effects (1 + 1 will always be 2 or rather x + y will always be the sum of x and y)


How closely the language adheres to that determines its purity. Haskell is pure; Clojure and Scala are not. I personally like how Clojure handles state and mutable objects which is why I've chosen to learn it.

Python has lambdas and you could definitely do FP in Python, but it would only be by convention. Since Python doesn't force you to think in a functional way, it might not be the best language for learning FP. That was also the reason I didn't choose Scala to learn FP, I wasn't having to think differently about my problems. Using Clojure has started to transform the way I approach problems.

Keep in mind these are just my own personal opinions, individual results may vary.


When all you have is a hammer, everything looks like a thumb.
Rick Goff
Ranch Hand

Joined: Nov 14, 2006
Posts: 54

Check this out: http://en.literateprograms.org/Fibonacci_numbers_(JavaScript) . Very helpful, imho.

Watch Odersky's lectures from the Coursera course. And make sure you download that course's dev environment. It comes with something he calls the "Worksheet" which is a fantastic working environment. It is the Scratchpad promised by the brochures.

I signed up for Dan Grossman's Coursera course but life has intervened and I haven't been able to keep up with the workload. I intend to try again, though. He starts from the ground up, beginning with the very good advice to set aside what you know from imperative languages and let him teach you FP as if you've never considered coding before.

I find a lot of value in the articles that have permanent links on the front page of clojure.org. Rich Hickey is a deep thinker, if I am any judge.

I hope the mention of the articles and videos above will encourage those who don't know about making a book-length commitment.

Many revered authors such as Bear Bibeault, Jon Resig and Douglas Crockford declare Javascript a functional language sired by Scheme. Bibeault and Resig's recent title, _Secrets of the Javascript Ninja_, is exactly an invitation to reconsider Javascript as a functional language, and a damn enticing one. Crockford's book, _Javascript: the Good Parts_, is a very good defense, too. (And his Google talk of the same title is tv worth watching.) Of course these books are about just the one language but they contain plenty of explanation and illustration of basic functional techniques.

Read _Land of Lisp_ by Conrad Barski, which teaches Common Lisp from square 0, and is a pleasure to read. _Real World Functional Programming_ contains some excellent explanations. Its examples are in C# and F#. Once you've swallowed the Kool-Aid, read _The Joy of Clojure_.

Brian Marick has written a book called _Functional Programming for Object Oriented Programmers_. The sample chapter is very good - very, erm, terrestrial. Check that out at leanpub. And Dean Wampler wrote an O'Reilly book similarly titled.


OCPJP 6
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Thanks for some great suggestions, Rick.

By the way, I can thoroughly recommend the Coursera Programming Languages course if you can manage it next time it runs. It's pretty much all about FP so far, initially with SML (which was clearly an inspiration for Scala), and now about to move onto Racket (Scheme Lisp). As a relative newbie to FP - despite completing the FP in Scala course - I'm finding it hard work but it's good fun and very rewarding.
 
Don't get me started about those stupid light bulbs.
 
subject: need advice on learning functional programming