Functional Programming

Programming Windows Azure

December 30th, 2009 / Development in a Blink

Available for pre-order. The author, Sriram Krishnan, has a blog. Plus he has posted Erlang and Python bindings for the Windows Azure storage API.

It’s not often that you get to spearhead the creation of a major new Microsoft API and it feels great to have seen this through from beginning to end

Functional Homework

December 25th, 2009 / Tech Tock

MejerTShirtAs I mentioned last month, I want to learn a functional programming language.  Lucky for me, Dr. Erik Meijer (and his electric t-shirt) gave a course on Haskell in October and its posted on Channel 9.

I watched the first lecture over a week ago but haven’t had time to do the homework till now.

Here it is in C#.  Its quicksort written as he described it in Haskell.  It doesn’t feel that different from the programming I usually do, but it is recursive (which I generally avoid), so maybe that’s something.  At least I did my homework, now I can go on to the second lecture :) .

Considering Adding a Functional Language to My Bag of Tricks

November 13th, 2009 / Tech Tock

There are a lot of people @Lab who know things like Erlang, Haskell, Python, R, etc., etc., etc.  I’m actually considering learning Lisp, but I need a project.  I “learned” php for a project over the summer, but I didn’t really like it since its so much like classic ASP (really messy).  Maybe I’ll learn F# instead…  That shouldn’t be such a strech since I built an ML compiler in grad school (F# is based on ML).  I wonder what kind of project I’d be interested in would benefit from a functional language…  I suppose I’ll have to read up on the language and maybe that will give me some ideas.

How to use MongoDB from PowerShell and F#

October 25th, 2009 / Development in a Blink

Dwight Merriman, founder of 10gen,  presented today, at Lab49 where I work, about his new database platform:

Mongo (from "humongous") is a high-performance, open source, schema-free document-oriented database. MongoDB is written in C++ and offers the following features:

Don Syme’s F# deck from JAOO

October 12th, 2009 / Development in a Blink

Don Syme, creator of F#, posts his F# tutorial he presented at JAOO 2009. Over 100 crunchy slides.

Some highlights of the code are:

  • Foundational material on F# async programming, and samples including:
    • Fetching web pages in parallel
    • The Bing translator sample using async programming
    • A Graphical, asynchronous twitter client in 50 lines
    • A Twitter feed example that uses F# first class reactive event processing to process the stream of feeds
    • An asynchronous web crawler
    • Processing images in parallel, using a mixture of I/O and CPU parallelism
  • An updated version of the famous Interactive DirectX demo

F# Integrated with Visual Studio 2010 and .NET 4

October 10th, 2009 / Development in a Blink
  • .NET 4 core types that F# uses, such as Tuple, Lazy, and BigInteger, are now part of the .NET Framework and can be used across all .NET languages
  • One ground-breaking feature of F# is Units of Measure, which allows you to annotate your floating point code with units, such as meters and seconds providing compile-time checking
  • F# language features are designed to make parallel and asynchronous programming more intuitive
  • F# is a strongly-typed language like C#, but with a lightweight syntax often seen in a dynamic language like Python

via F# in VS2010

IFL 2009: First Impressions

October 4th, 2009

Last week, my colleague Ken Overton and I attended the IFL 2009 conference in South Orange, New Jersey.  The event was partially funded by Jane St Capital, and consisted of research on the implementation and application of “functional languages.”  The invited talk was by Benjamin Pierce (of TaPL fame), on his joint work (with Nate Foster and others) on the bidirectional programming language Boomerang.  The conference’s full range of subjects was diverse, and in this article I will summarize what I saw.

IFL 2009

September 27th, 2009

I spent part of last week attending IFL 2009; as I don’t really consider myself a ‘real’ functional programmer, what I have to say is kind of an outsiders perspective.

Generally, I thought it was worth 3 days of my life and saw some things that look pretty promising. It was also nice to get a window on the types of problems that the field is grappling with right now. Not surprisingly, there were several presentations on concurrency/parallelism; some were attempts to provide more syntax for explicitly declaring concurrent programs, but what I find more promising were the presentations of data parallelism where the language or library figures out what can be processed in parallel and optimally does it for you. Thus your programs address the problem you’re actually solving rather than concurrency arcana.

Why functional programming?

September 16th, 2009 / Learning in progress

Functional programming is not a silver bullet, but learning it will indeed add to the knowledge of every software engineer about solving problem in different a way. One can always implement functional style of programming in any imperative language they use.

Advantages:

  1. For analysis of mathematical problem that requires human analysts, it is possible to represent the problem in a concise mathematical form that analysts can understand.
  2. A declarative language simplifies the creation and multiple interpretations and static analysis.
  3. Compositional construction allows natural mapping of the problem domain and encourages code reuse.

Reactive Programming: How to Invert an Enumerator

July 29th, 2009

“The opposite of a correct statement is a false statement. But the opposite of a profound truth may well be another profound truth.”

Niels Bohr

Enumerables and Enumerators, oh my!

Enumerables and Enumerators are great; big sacks of data we can iterate through using a well-established method. Just in case anyone isn’t up on their GOF, the pair looks a bit like this:

interface IEnumerable<T>

{

    IEnumerator<T> GetEnumerator();

}

interface IEnumerator<T> : IDisposable

{

    bool MoveNext();

    T Current { get; }

Why learning F# difficult.

May 26th, 2009 / Learning in progress

i found this really gr8 blog post and want to share.

http://www.gotnet.biz/Blog/post/Why-Learning-FSharp-Is-So-Difficult.aspx thanks to Kevin Hazzard for wonderful post.

Differentiating Types in Haskell

April 26th, 2009

In the last article, we explored the concept of data-types as algebraic expressions, and the idea that the derivative of these expressions produces their type of one-hole contexts.  This “leap of logic” gives us a firm foundation for purely-functional incremental mutation, and (more generally) proof that there are interesting things happening at the level of types!

Still, many good ideas are destroyed (or exalted) when the time comes to make a real program out of them.  So in this article, we will write that program, and in the process shore up this notion of a derivative by defining it over any recursive type.

The Algebra of Data, and the Calculus of Mutation

April 18th, 2009

With the spreading popularity of languages like F# and Haskell, many people are encountering the concept of an algebraic data type for the first time.  When that term is produced without explanation, it almost invariably becomes a source of confusion.  In what sense are data types algebraic?  Is there a one-to-one correspondence between the structures of high-school algebra and the data types of Haskell?  Could I create a polynomial data type?  Do I have to remember the quadratic formula?  Are the term-transformations of (say) differential calculus meaningful in the context of algebraic data types?  Isn’t this all just a bunch of general abstract nonsense?

We’ll investigate these questions, and perhaps demystify this important concept of functional languages.

Pulling data from USASpending.gov

April 14th, 2009 / Learning in progress

So i found this great post by doug finke on his blog where he has show implementation for retrieving data from usaspending website using Power Shell and LinqToXml here are my implmentation on C# and F#

ccode1

 

 

 

 

here is F# implementation.

 

fcode1

MapReduce in F#

April 14th, 2009 / Learning in progress

I came across the following link on MapReduce implementation in F#

http://www.twine.com/item/1229c60s4-4q/exploring-mapreduce-with-f

Also Microsoft is working on C# implementation known as LINQ to DataCenter.

For those who don’t know MapReduce should think twice as anyone using google search is the user of MapReduce algorithm.

Wikipedia has clear explanation of the MapReduce http://en.wikipedia.org/wiki/MapReduce

Functional Fibonnaci series in C#

April 14th, 2009 / Learning in progress

C# code for functional fibonnaci numbers

Func<int, int> fib = null;
fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

Okay so you guys would think why would i declare the delegate in one line and instantiate in the next. The reason is that if you do everything in same line complier wont recognize the recursive call using the delegate. Try this 

Func<int, int> fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

Above code wont compile 

Purely-Functional Incremental Mutation

April 5th, 2009

In writing functional programs, we’ve learned to put aside many problematic techniques for writing programs, especially iteration and mutation over data structures.  On the other hand, some folks aren’t buying in to this functional nonsense, and they continue to build elaborate (and working, they will remind you) software based fundamentally on algorithms incrementally mutating data structures.

Can’t we all just get along?

(An answer inside, plus the continuation of our exploration of type-classes.)

Does Haskell Support Subtyping? It Depends.

March 28th, 2009

We’ve already seen that type-classes enable a very rich form of ad-hoc overloading (especially for basic arithmetic, where other languages require awkward workarounds).  If that were all that we could do with type-classes, it would more than justify their existence in a language.  However, that’s not all that we can do with type-classes — they’re good for much more.

Functional Arithmetic Redux

March 24th, 2009

Last time we compared arithmetic overloading and type-inference in F# and Haskell.  We even created our own numeric type in Haskell, as represented by Peano numbers, by implementing a type-class instance.  This stood in contrast to F#’s fixed set of numeric types (whose overloading of arithmetic functions was treated as a special case by F#’s type-checker).

APL/KDB programmers don’t have the benefit of a type-checker, but they do have some sophisticated overloading of arithmetic types.  Here we’ll see how we can recover APL-style arithmetic in Haskell, properly typed, and discover an important generalization that should further the argument that type-classes would be a valuable addition to F#.

The F# overload-o-phone

March 20th, 2009

By now, you’ve probably heard the standard line.  Functional programming is all sweetness and light; if we merely cast off the detritus of bad habits we’ve learned from C, C++ and Java we’ll find ourselves in a different world, a better world.  Without state, and by dint of a civilized logical system of analysis, all wishes will come true.

Yet, in the world of F# (and OCaml in a slightly different case), we encounter some logical anomalies:

> (+);;
val it : (int -> int -> int)
> 3.2 + 5.4;;
val it : float = 8.6

Which is the truth here?  Initially we’re told that the “+” operator expects two int values, but it seems to happily accept two floats.  Worse yet, if we expect to rely on this magical addition, our hopes are soon dashed:

> let twice x = x + x;;
> twice 9.4;;
  ——^^^
stdin(2,5): error FS0001: This expression has type
    float
but is here used with type
    int.

Our only chance for generic arithmetic (over ints, floats, vectors, etc) is to explicitly pass the necessary arithmetic functions around:

> let twice fn x = fn x x;;
val twice : (’a -> ‘a -> ‘b) -> ‘a -> ‘b

> twice (+) 1;;
val it : int = 2
> twice (+) 1.2;;
val it : float = 2.4
> twice (fun (a,b) (c,d) -> (a+c,b+d)) (1,2.3);;
val it : int * float = (2, 4.6)

And although this works, it has a kind of Jerry Seinfeld “why don’t you just tell me the function you want to call?” feel to it.  There must be a better way.