Hiatus (agh) wrote,
Hiatus
agh

Functional Programming in the mainstream?

Expert C++ developers have long known how to program with a functional programming style by applying some nice libraries and a few good principles and design patterns.

The most basic principle is, of course, don't have functions with out-parameters; prefer to use return values for better composability.  And of course, you have to have exceptions that can be propagated outside the return flow.  (So there goes C right away.)  Add on using delegates (pointers-to-member functions) and functors (objects that behave like methods by overloading the () operator), and you're on your way, though there are pitfalls you need to navigate.

One big aspect of FP, however, is having the compiler make types for you on the fly, such as tuples.  C++ can't easily do that, though, you could kinda work something out using templates by having templated structs, one each for varying numbers of arguments, and constructors that combine answers into those structs... but it would be ugly.

C++ also doesn't have anonymous delegates, which means, gosh, you have to go through the tedium of naming and finding a home for all your functions.

Enter C# 3.0, out in public beta right now.  Due to LINQ (language-integrated query) they needed to add all these FP features into the language.  So now it has anonymous types, lambdas, and expression trees!  This blog, though a couple years old, offers a good walkthrough of some of the new stuff.
http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees

I should mention that the anonymous types are still statically typed by the compiler.

I hope this means I can one day use FP in my day job!

Tags: code
Subscribe

  • Windows 7

    Windows 7 is coming out in beta this weekend. It's a no-brainer upgrade from Vista, as it is, for my purposes, a strictly superior OS. And if you're…

  • Pro and Repro were sittin' in a boat... Pro fell out, who was left?

    So, I'm debugging this issue with the tooltips in the Windows common controls. A particular debugger is crashing when it's trying to display the…

  • F#MiniJava

    Sunday, Chris and I finished our minijava compiler implementation in F#. Whereas most of the class implemented it in Java or C#, we knew that all we…

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments