Posted: Thu Feb 24, 2011 4:11 pm Post subject: Functional Programming
Hello, all!
I'm really interested in learning some of the fundamental concepts of functional programming, mainly to add some breadth to my computer science knowledge. Any suggestions on a good language to start with? (I know Scheme is taught at University of Waterloo, but are there perhaps other languages that might give me a stronger knowledge base?)
Sponsor Sponsor
yoursecretninja
Posted: Thu Feb 24, 2011 6:36 pm Post subject: RE:Functional Programming
If you've worked extensively with imperative programming languages (i.e. most mainstream object-oriented and procedural programming languages) than functional programming is a bit mind-boggling. At least, I found it to be when we studied it in class. Switching from imperative to functional was a bit of a culture shock in away. I still don't feel comfortable with functional programming at all, but I would like to learn it deeper myself, especially since functional programming concepts are creeping into or appearing in mainstream languages (for example, Ruby has support for functional programming paradigms).
If you're coming from the imperative world, it's probably best to review the history of functional programming (LISP) as well as it's origin (lambda calculus) to understand what it's all about rather than dive right in, picking a language, and programming in it. Once you've got a feel for what functional programming is all about then take a look at common LISP implementations or derivatives, such as commonLISP and Scheme, as well as other functional languages like Haskell and ML and decide for yourself what seems best for you. Haskell seems to be particularly popular (or trendy). Having never worked with any of the languages more extensively than small school assignments, I can't recommend one over another. But then again, I'm always hesitant to recommend a programming a language as it should boil down to requirements, preferences, and experience.
Cheers
Tony
Posted: Thu Feb 24, 2011 6:46 pm Post subject: RE:Functional Programming
Python also allows for functional programming. You can leverage that familiar environment to get some experience with the basics, before diving into exclusively functional languages.
Posted: Fri Feb 25, 2011 2:57 pm Post subject: Re: Functional Programming
That's great, thanks guys. I'm gonna look into Python's ability to support FP and try to familiarize myself with some of the concepts, and then I'll take a look at some options for trying purely functional languages.
apython1992
Posted: Wed Mar 16, 2011 1:20 pm Post subject: Re: Functional Programming
I'm starting to really enjoy using what support python provides for functional programming: lamdba functions, map, filter, etc. Now that I'm rather comfortable with these, would mapping a function over a sequence be more efficient speed-wise than simple list comprehensions? For example, would:
code:
cubes = map(lambda x: x*x*x, range(10))
run faster than (with perhaps larger lists)
code:
cubes = [x*x*x for x in range(10)]
?
I like using these functional tools, but it would be interesting to know whether they have some advantage besides being concise.
DtY
Posted: Wed Mar 16, 2011 1:39 pm Post subject: RE:Functional Programming
The problem I had with Haskell was that it had way too much syntax. I found that I wasn't really learning anything useful, just some syntax.
Mostly it was weird indentation stuff, I seem to remember having trouble figuring out how where blocks should be indented.
The problem I had with Lisp is that it *doesn't* have syntax. It's pretty cool, though.
---
I'm also interested in which of those would be more efficient in python. I imagine the list comprehension stuff would be since it's built in, whereas the functional stuff are just higher order functions, and they also have to construct a lambda function (the list comprehension might compile down to something similar, though).
apython1992
Posted: Wed Mar 16, 2011 1:46 pm Post subject: Re: RE:Functional Programming
DtY @ Wed Mar 16, 2011 1:39 pm wrote:
I'm also interested in which of those would be more efficient in python. I imagine the list comprehension stuff would be since it's built in, whereas the functional stuff are just higher order functions, and they also have to construct a lambda function (the list comprehension might compile down to something similar, though).
What do you mean by built-in? The functional method is maybe slowed down by having to create a lambda function, but I read somewhere that for loops tend to cause things to slow down as well...though I don't know enough about the internal workings of the language to know whether or not map ends up using a for loop anyway...
yoursecretninja
Posted: Wed Mar 16, 2011 1:49 pm Post subject: Re: Functional Programming
Not sure if mapping would be faster. An uneducated guess would be that it would add overhead (more on the stack)??? Would be interested to know.
As for the advantages of some functional "tools" or techniques or paradigms or whatever you want to call it:
Lambdas (sometimes called blocks) allow you to nest functions within functions. The nested function (lambda or block) has access to variables within the scope of the outer function plus it can have its own instance variables. This can be very beneficial if you don't want other parts of your code to access a function. It's also very useful when you want to isolate some code into a function to avoid duplication but you only need to use the function in one specific place. Another place it is very useful is in keeping related code close together. For example, you may need to provide a function to a listener, but with lambdas or blocks, you can just write the function in write in the place where the listener is.
One place where you will see a lot of this is in JavaScript where it is very common practice to place your functions within other functions to prevent them from clashing with other scripts. It can also be very handy in callbacks. Another place you'll see blocks used a lot is in iOS programming where you frequently need to provide functions to various types of event listeners or as callbacks but the code may not be very usable elsewhere in the program.
Purely functional languages are advantageous in that they make concurrent processing a lot easier. Because you are not operating on objects or variables you can rely on the state of the program.
Sponsor Sponsor
apython1992
Posted: Wed Mar 16, 2011 2:10 pm Post subject: Re: Functional Programming
That's good to know. Any suggestions on where I could maybe go next to explore a bit more FP outside of the little that Python has to offer?
Shanethe13
Posted: Wed Mar 16, 2011 4:41 pm Post subject: RE:Functional Programming
The first dives right in, and covers more practical applications of the stuff, whereas the second takes things more slowly, and delves a bit more into the theory behind some of the concepts.
apython1992
Posted: Wed Mar 16, 2011 4:52 pm Post subject: RE:Functional Programming
Thanks! I'm really wanting to go through the theory so that second tutorial is perfect.
yoursecretninja
Posted: Wed Mar 16, 2011 6:34 pm Post subject: RE:Functional Programming
Thanks for posting that learn you Haskell link. I took a quick skim through it and it looks really good. Bookmarked it in my 'to read this summer' list
Shanethe13
Posted: Wed Mar 16, 2011 8:13 pm Post subject: Re: RE:Functional Programming
apython1992 @ Wed Mar 16, 2011 4:52 pm wrote:
Thanks! I'm really wanting to go through the theory so that second tutorial is perfect.
No problem! I'd recommend going through both at more or less the same time though. I made the mistake of following the second almost exclusively, and by the time I finished I could throw around nifty terminology like monad or functor or zipper, but I didn't really have any idea of how to put the concepts together to actually do something. The second is my personal favourite, and should be able to explain some of the weirder ideas, but definitely take a look at the other link if you need to see something in action.
btiffin
Posted: Fri Mar 18, 2011 3:08 am Post subject: Re: Functional Programming
Old guy advice;
They are not "purely functional", nor can they be called "functional" really, but to look at things from a different angle if you are looking for breadth of coverage; Forth, REBOL, Icon.
Chuck Moore, Carl Sassenrath, Ralph Griswold. They have given the world a lot. With side effects a-plenty.
Forth is word and stack based, not functional at heart, but your high level dictionary can be made to look and feel like it.
REBOL is functional, not really, but side effects can be as "bad" as an executable word changing it's own and other's behaviour. (Perhaps a reason to look at REBOL is to gain a healthy respect for referential transparency if you find FP fits your development psyche.)
Icon, just because. Goal directed evaluation and generators.
Cheers
DtY
Posted: Fri Mar 18, 2011 10:25 am Post subject: RE:Functional Programming
Speaking of Forth, Joy is a purely functional language that took a lot from Forth. I haven't been able to find too much information about it, but it looks pretty cool.