6 ms·
Functional programming for Object Oriented programmers
With the continuing popularity of functional programming, it doesn't look like it's ever going away. Here's is an introduction to functional programming in F# written with the object oriented (C#) programmer in mind.
- marshallp 15y agoOne thing missed in a big way by this article and other newbies when it comes to (static) functional programming is the centrality of types. It really is what makes ml-derived languages special and it's a shame it's not promoted to non-functional programmers more. Most of your work is related to specifying your types, the rest falls into place naturally, and is why you can think and write code elegantly (and is super fun).
- lifebeyondfife 15y agoThanks for the feedback. To be honest I haven't really grasped all the ins and outs of functional programming yet so tried to ignore it as a first step. From what I've seen so far, I do like how much all the typing takes care of itself.
- mushishi 15y agoI would really like to hear more about this. I also do type-oriented programming in OO languages, such as Java. The code naturally falls into place, too, but it's just because of Java's verboseness, you end up a lot of hand-waving. But I don't think it differs much from how I would program in functional style, say, in Scala.
- marshallp 15y agoit all depends on your problem. for gui programming oo code is natural, for transforming complex data structures ml is natural. transforming xml is a horrible mess in java, so xslt is recommended, and xslt is pretty much ml (one of the creators of haskell was a contributor to xslt). it's not matter of java verboseness, even python or lisp isn't well suited to dealing with complex datastructures because they lack the pattern matching over compiler enforced datastructure machinery.
- jberryman 15y agoThis talk by Simon PJ on type classes might be good http://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but-not-as-we-know-them http://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but...
- mushishi 15y agoI actually have that on a browser tab, and will take a look at it. I know some Haskell (have read maybe 4 books but haven't really ever wrote it).
- AndrewDucker 15y agoMy problem with these things (and I'm not picking on this particular article, just on introduction to functional programming articles in general) is that they always tell you how awesomely easy it is to make lists, or calculate the fibonacci sequence, two things I never have to do in the office. What I want to know is whether functional programming languages would help me to write UIs better than C# does, to create XML documents from data, to retrieve data from web APIs and then display the results, etc. These are things I have a need to do on a daily basis, and I'd like to know if F# is the tool for them, or if I should stick to C# unless I'm writing a list processor or calculation engine.
- lifebeyondfife 15y agoI can't really speak from any experience as I've only just started programming in F# myself, I think for the most part it's not meant to replace the things you mention. The reason I looked into it initially was because of its applicability with mathematical calculations and parallelism. Because of the .Net angle though you can mix and match your projects on Windows systems. Here's a Stack Exchange question about what proficient F# programmers use it for: http://stackoverflow.com/questions/568827/what-areas-of-code-are-you-using-f-for http://stackoverflow.com/questions/568827/what-areas-of-code...
- tomjen3 15y agoI am not quite sure you don't make lists on a daily basis. I do it all the time when I program -- and I would guess that most people do so too. Calculating the Fibonnaci sequence is really not that interesting, true enough, but it serves as a relatively simple way to demonstrate recursion (please don't claim you never use it, it is just masked as loops in C#). I find it funny that you want to retrieve data from web APIs and create XML ducuments -- which is a case where you would often use lists (e.g an XML document is essentially a tree which is just a list of trees and nodes) -- so any language which makes it easy to process list would make you more productive in solving exactly the problems you want it to, if only you would learn it.
- AndrewDucker 15y ago
- itechnofuture 15y agoThis is helpful for beginner or a bit upper [about functional programming] to get a few concept i think. For that i hope it would be very helpful informative things for OO Programmers.
- Zak 15y agoThe article says List.map function list - Invokes function on every item within list and returns nothing. which isn't what map does in other functional programming languages. Applying a function and returning nothing isn't a very functional thing to do in general, as whenever possible functions should be called for their return values, not anything else they might do[0]. Map normally returns a list of the function applied to every element of the list. I looked up the documentation for F#'s map, and it appears to do the same thing: http://msdn.microsoft.com/en-us/library/ee370378.aspx http://msdn.microsoft.com/en-us/library/ee370378.aspx [0] And when possible, a function should only return a value, not do other things.
- xentronium 15y agoYeah, it's clearly an error on author's side.
- lifebeyondfife 15y agoYes, my mistake sorry. Correcting it now.
- xentronium 15y agoI would recommend "Why functional programming matters"[1] paper to anyone struggling to understand the purpose of functional programming. It's only about 20 pages. [1] http://web.mst.edu/~tauritzd/courses/cs347/whyfp.pdf http://web.mst.edu/~tauritzd/courses/cs347/whyfp.pdf
- Confusion 15y agoThat paper is a surefire way to scare people and keep functional programming from those that could use it perfectly well. You, and many others, vastly overestimate how capable even highly educated programmers are in understanding the arguments made in such a technical text.
- mjbellantoni 15y ago"Functions are invoked without using brackets or commas" Is this really a strict property of functional languages? :)
- nandemo 15y agoYeah, the post is mixing essential features of functional languages with incidental points about a specific language's syntax. It's not wrong but somewhat misleading.
- lifebeyondfife 15y agoI hope it comes across in the preamble that I've been using F# (the only functional programming language I've ever tried out) for just a few days and as such I simply don't know which is which yet. Anybody who's read this blog post and is interested enough to look further into functional programming should (a) download LINQPad and play around with the interactive F# tutorial and (b) start reading things by people with a few years of functional programming experience under their belt, not a few days.
- nandemo 15y agoFair enough. Sorry if my comment came out as negative, I simply meant to elaborate on bellantoni's comment. Minor inaccuracies aside, if your post instills the curiosity of a few programmers out there, it's a good thing. Please keep writing. :-)
- lifebeyondfife 15y agoNot at all, I'm really happy it's garnered so much interest and I'm glad people have taken the time to help correct me / advise what's not quite right.
- josh5555 15y agogreat article...very useful. tyvm!