A bit on making functions tail-recursive in F#

If you recall a while back when I was demonstrating some Functional Data Structures, I mentioned the fact that some of the functions were not tail recursive, and that this is something that we would probably want to do something about. Which raises the question: How exactly do we go about making a function tail-recursive? I am going to attempt to address that question here. One of the first problems with creating a tail recursive function is figuring out whether a function is tail recursive in the first place. Sadly this isn’t something that is always obvious. There has been some discussion about generating a compiler warning if a function is not tail recursive, which sounds like a dandy idea since the compiler knows enough to know how to optimize tail recursive functions for us. But we don’t have that yet, so we’re going to have to try and figure it out on our own. So here are some things to look for: ...

14 June 2015 · 5 min

fold - The greatest thing that ever happened to your data structure

Let’s say that you’ve been working hard on this really awesome data structure. Its fast, its space efficient, its immutable, its everything anyone could dream of in a data structure. But you only have time to implement one function for processing the data in your new miracle structure, so what would it be? Ok, not a terribly realistic scenario, but bare with me here, there is a point to this. The answer to this question, of course, is that you would implement fold. Why you might ask? Because if you have a fold implementation then it is possible to implement just about any other function you want in terms of fold. Don’t believe me? Well, I’ll show you, and in showing you I’ll also demonstrate how finding the right abstraction in a functional language can reduce the size and complexity of your codebase in amazing ways. ...

8 June 2015 · 5 min

Purely Functional Data Structures–Part 2

So here we are at part 2 in the series of posts looking at Functional Data Structures from the book of the same name by Chris Okasaki. Last time we looked at what is perhaps the simplest of the functional data structures, the List (also useful as a LIFO stack). Up next we’ll continue in the order that Chris Okasaki used in his book, and take a look at implementing a Set using a Binary Tree. ...

24 July 2012 · 6 min

A quick (?) retrospective on learning (and using) F#

As you may have guessed from the title, I’ve started doing some work with F#. Initially I was somewhat reluctant to go down the F# path because some of the more interesting aspects of the other functional languages I’ve been exploring are not present…specifically the type systems behind Scala and Haskell, the laziness of Haskell, and the concurrent programming model of Erlang. In spite of these perceived downfalls, there were some definite plusses, namely interoperability with everything .Net, immutability by default, and the wonderful concise programing model of a functional language. ...

22 July 2012 · 6 min

Sometimes doing the right thing is still not the right thing

Tentatively subtitled: “How scale can make fools of us all” This is going to be a real life war story…cause I haven’t done one of those in a while, and this particular case really ticked me off. Here’s the scoop: I’ve got a “service” which is called by other parts of the system. And by “service” I don’t mean something running in its own process and waiting for SOAP/REST requests or messages, I simply mean something that has a defined entry point (a static method in this case), where you pass in some data, and get something back. ...

20 March 2012 · 6 min

Announcing the Duhking Library for .Net 3.5

If it walks like a giraffe and talks like a duck then what is it? Maybe a duhk? Who knows, but it certainly is not a duck. So if that is the case, then you can probably guess what the Duhking library is all about…or maybe you can’t. In terms of programming, Duck Typing refers to the ability of some languages to allow you to treat an object of one type as an object of a different type, provided the methods/properties needed exist on both objects. Statically typed languages are usually not very good at this sort of loosey-goosey type inference, which is why this behavior is typically restricted to languages with less stringent rules on typing. ...

17 June 2010 · 6 min

Announcing CR_MoveFile: DxCore plug-in for moving files around in a solution

As of right about now, you should be able to mosey on over to the DxCore Community Plug-ins page, and grab a copy of CR_MoveFile. This is a plug-in I created primarily as a tool to aid in working in a TDD environment, but which certainly has uses for non-TDD applications. It does basically what the name suggests, it allows you to move a file from one directory in your solution/project structure to another, even one in a different project. I implemented this as a code provider (since it could change the functionality if you move the file from one project to another), so it will appear in the Code menu when you have the cursor somewhere within the beginning blocks of a file (“using” sections, namespace declaration, or class/interface/struct declarations). Once selected you are presented with a popup window which has a tree that represents your current solution structure, with your current directory highlighted. You can use the arrow keys to navigate the directories and choose a new home for your file. ...

7 December 2009 · 3 min

Getting a CodeRush: Inside a CodeRush CodeIssue

Anyone who has been around me for more than a few hours while coding, or who pays any attention to me on Twitter will know that I am a huge fan of CodeRush and Refactor Pro! from DevExpress. I consider these sorts of tools essential to getting the most out of your development environment, and I think CodeRush is one of the best tools available for a number of reasons, not the least of which is it’s extensibility. CodeRush is built on top of DxCore, which is a freely available library for building Visual Studio plug-ins (incidentally, DevExpress also have a free version of CodeRush called CodeRush XPress, which is built on the same platform). DxCore provides any developer who wants it access to the same tools that the folks at DevExpress have for building plug-ins and extensions on top of VisualStudio, and several developers (including yours truly) have done just that. ...

7 October 2009 · 13 min

Some code for sorting lists which may contain dependencies

I ran into this odd problem recently working with some Linq2SQL based persistence code. There is some code someone put together to commit a list of changed entities to the database as part of a single transaction, which simply iterates through the list and performs the appropriate action. The problem I was having was that I had an object referenced by another object that needed to be persisted first, otherwise there was a foreign key violation. To add to the strangeness there seemed to be some magic going on (most likely utilizing the INotifyPropertyChanged goodness), so that even if I tried to persist just my dependent object first, both were still showing up in the list, and always in exactly the wrong order. Now, I’m okay with magic. Magic makes a lot of things a lot easier. The problem arises whenever the magic is incomplete, and doesn’t follow through to take care of all of the operation. Its like someone comming up to you and saying “Pick A Card”, at which point you do, and put the card back, and they say “I know what your card was” and walking away. Not real convincing. This is what was going on here. There was the smarts to know that changes were being made to more than one entity, and there were even attributes to define what properties contained dependent objects, but no smarts to actually deal with a case when you would want to save more than one object in an object graph at a time. ...

18 September 2009 · 4 min

Review: Art of Unit Testing by Roy Osherove

I was pleased to find recently that Roy Osherove’s Art of Unit Testing was available on Safari. I have been following Roy’s blog for a while now, and was quite excited at the prospect of him writing a book on Unit Testing. It was only my personal cheapness that kept me from shelling out the $25 to get the E-Book version from Manning ahead of time. I have to say, now that I have read it, that it would have been well worth the money. Before I get too deep I want to provide some context for what I am about to say. ...

2 September 2009 · 7 min