Accounting for Cost of Sales with Currying

I have a quick question for you. If you’re selling a widget for $100 and the local sales tax is 8%, how much should you add to the cost of the item to cover the sales tax? If you said $8 or 8%, you’re losing money. To the tune of 64 cents a sale. (Do the math, 8% of 108 is $8.64.)

What’s 64 cents you ask? Over a hundred sales it’s $64, over a thousand it’s $640. And over a million sales it’s $640 thousand dollars! Granted, in the process of losing that 640 thousand dollars, you’ve made $108 Million in pre-tax revenue. But more than half a million dollars in lost profit is nothing to scoff at either. But that’s what every POS system out there does. I’ve just checked in the code for Azure POS (the Silverlight/.NET 4/Azure exemplar I’m developing for my Mix content submission) that does it right. The functionality gave me a good opportunity to demonstrate simulated currying in C#.

Indian Food, Lambdas, and Sales Tax

Currying is a feature of most purely functional languages (named after Haskell Curry) whereby a function that takes two parameters is transformed into two functions, the first function takes the first parameter and returns the second which takes the second parameter and returns the result. In fact most functional languages, multi-parameter functions are at their core represented just as that. To clarify let’s look at declaring a multi-parameter function in F#:

let applyTax(taxRate:double) (amount:double)
  //insert computation here and return result (declared as a double
  result;;

In C# this is equivalent to

public double applyTax(double taxRate, double amount)

Well not really. There’s a significant difference between the two and this is where currying comes in. The declaration in F#, when entered into the F# Interactive will give the following output:

val applyTax : double->double->double

This is read as “the value applyTax is a function that takes a double that returns a function that takes a double that returns a double.” So for instance if you know the tax rate (say .08) you can pass it to applyTax and have a handle to a function that will take the amount and apply an 8% tax to it. F# is pretty cool like that.

Getting Func<e> With It

So how can we do that in C#? Simply make an overload of ApplyTax that returns a func<double,double>

public func<double,double> ApplyTax(double taxRate)
{
    return (amount)=>{ApplyTax(taxrate,amount);}
}

Now I can create variables for the different tax rates at my various retail stores

var applyCookCountyTax = ApplyTax(.10);
var applyLakeCountyTax = ApplyTax(.08);

This is the plight of retailers in the Chicagoland area. There is a 10% sales tax in Cook county. That means you’re paying $20 more to buy a $1000 TV in Cook County than in Lake County and retailers close to the county line lose sales to this disparity. In reality you’d probably store the functions in a Dictionary rather than holding them in individual variables.

var taxAmount = store.TaxTable[“Cook”](100);
//taxAmount =8.70

The Secret Sauce

The solution to the question is simple algebra let x equal the amount of the sale and z be the tax rate you need to find y such that when x and y are added, multiplied by z and subtracted from their sum you get x.

x+y – z(x+y) = x
x+y – zx – zy = x
y-zy = zx

Given x and z solving for y is trivial. Now you can wear a smug smile when you make a purchase at Best Buy knowing that you’re not footing the bill for sales tax. That is until you realize that the REAL sales tax is the markup they add over their wholesale price for that TV you just bought.

Extra Credit

Wes Dyer has an interesting post about simulated currying in C# on his blog. Also I’d imagine that using dynamics it might be possible to declare a base class that auto curries any functions declared in

Published Monday, January 18, 2010 9:00 AM by Mike Brown

Leave a Comment

(required) 
(required) 
(optional)
(required)