Project Euler, Python, F# and Clojure

A rather ambitious title but apropos of what I’ve been playing around with this morning. At this month’s Dallas Hack Club, several of us got together and essentially toyed around with problem #1 on Project Euler, learning new things as we went. I ripped through it in Python pretty quickly mostly because I had done it before but partially because Python makes things so easy. After that, I started trying to do it in Clojure, a language I’m interested in but can’t seem to find the time to truly concentrate on.

My initial solution in Clojure was both ugly and uncompilable making it reasonably useless. I’ve been struggling with getting my head truly around functional programming and I finally figured that the solution to that was to do more of it. This summer, in another fit of ADHD and boredom, I bought the electronic version of Chris Smith’s Programming F#. Like most of my projects, little came of it until last night when I decided to work really hard on it for 30 minutes. I figured out Euler’s problem 1 in about 20 minutes and that made me think I should be able to handle Clojure as well.

It took a lot longer than 30 minutes but I finally figured it out in a way that didn’t require 85 lines of Clojure. I think the functional style is starting to sink in. Doing both F# and Clojure seems to hammer home the concepts because I can develop a solution in F# in a medium I’m comfortable in (.Net and Visual Studio) and then move to Clojure with a little more background to build from. The solutions are provided below as a reference.

def main():
  sum = 0
  for x in range(1, 1000):
    if x%3==0 or x%5==0: sum+= x
  print sum

This was the original solution in Python. After doing the F# and Clojure work, I came up with something shorter if slightly more obtuse:

reduce(lambda x, y: x+y, filter(lambda x: x%3==0 or x%5==0, range(1, 1000)))

Those two lambdas are not good for long term maintenance and aren’t that readable. A list comprehension in place of the filter would be a good deal clearer.

reduce(lambda x, y: x+y, [num for num in range(1, 1000) if x%3==0 or x%5==0])

So we’re down from 5 lines to 1 while retaining a good deal of readability. Maybe there really is something to this “Functional programming makes you a better programmer” stuff.

In unimproved F# (I’m open to suggestions):

let result (acc : int) x =
    if x % 3 = 0 || x % 5 = 0 then
        acc + x
List.reduce result [0..999];;

And finally, in what I think is decent Clojure:

(defn isdiv [x] (or (= (rem x 3) 0) (= (rem x 5) 0)))
(reduce + (filter isdiv (range 1 1000)))

Sorry for not having syntax highlighting for the Clojure or F# code, they would both be a great deal more readable. However, it’s interesting to see in Clojure how all functions are first class, e.g. I can pass the + operator to the reduce function instead of having to define a lambda as I did in Python.

I have a feeling the Clojure code could be more succinct but for a first working cut, I’m pretty pleased with it. It’s clean and easy to read, even for someone like me with little exposure to the prefix notation and all the parens.

Overall, I feel less dumb than I did when I woke up this morning. I’m guessing the beer I drink at tonight’s Halloween party will fix that though.

On Debt

When historians look back on the current time period, they will notice many things but I think the main thing they will find interesting and worthy of study is our penchant for debt. We, specifically Americans but generally all civilized people in this time period, thrive on debt of all kinds. It will be our eventual downfall and in fact, has probably already contributed to the beginning unless our paths are drastically altered. I typically think of debt as financial or monetary but as I was thinking about how to help a trainee today, I realized that we have debt of all kinds, an affliction that seems to run deep in our psyches. This has happened blindingly fast in a historical sense. Our grandparents were not like this. Even our parents, the boomers, were not so enchanted with debt though the problem certainly started during their youth and young adulthood.

We are certainly stricken by financial debt. Even though the American consumer has ratcheted down his spending and increased his saving over the past 18 months, we have such a crushing financial debt load that it will be years before the ship is righted and in fact may never be because of the combined problems of the amount of debt and our government’s desire to have us increase our debt in order to drive an increasingly fragile economy through our spending. Our government builds a huge monetary debt by effectively creating money out of thin air in a misguided attempt to save our economy. This money isn’t even serving the purpose of its intent as it sits on the books at the Federal Reserve accumulating interest instead of being lent out into the economy where it might actually have a positive effect.

However, we also have become addicted to physical debt. The great majority of us treat our bodies and our minds as if they were our credit cards, running up large balances with little thought for the highly negative effect this will have on our physical future. We eat poorly, we do not exercise, we sit for long periods, we sit nightly dumbfounded in front of the television or computer. This physical debt is a constant in many of our lives. We can do this because we have solved the physical problems of our grandparents in that most work we do now is not physical and does not force us to move in great amounts.

Once upon a time, all debt was hard, oftentimes embarrassing. To be in debt was to be stamped with a social stigma. Now, debt of all kinds is easy. It is easy to get a credit card and live beyond our means. It is easy to eat whatever we want and choose to never be in hunger. It is easy not to exercise to discomfort. And yet, by doing each of these things, we mortgage our future ability to live in comfort, either financially or physically. We look for easy ways out because we are unaccustomed to things being hard. We see this constantly writ large in our government where we are ruled by a political class focused on the short term, immediate return on sound bytes.

The great problem with debt is that it is always easier to get into it than it is to get out of it. The longer debt is carried, the harder it is to fight out of it. This is as true physically as it is financially. Once you choose to live beyond your means physically, it will become easier and easier to stay on a path of poor diet and no exercise. Physical debt is slightly more insidious than financial debt in that it is easier to ignore over the short and medium term. Of course, financially, you can declare bankruptcy and after some time, start over with a clean slate. Physical debt has no such possibility save death. Getting out of physical debt is always a difficult, painful struggle.

In the end, debt is the easiest way out in our current situation. It is hard to live within ones means financially because of the illusion of what being middle class means. It is hard to live within one’s means physically because it requires time and effort to eat well as a habit and to exercise at all, much less at the level required to avoid physical debt. Because of our physical debt as a nation, we can’t have an honest discussion about health care since there is little personal responsibility to stay debt free.

Eventually, debt will be what ends America as a power in the civilized world. It will likely be financial and monetary debt, debt run up by our government almost entirely without our consent. But our physical debt will likely play a part as our politicians race to create a new world order where everyone, deserving or not, has health care insurance. As with all debt, the responsible among us will pay the greatest toll.

Don’t Be Evil A Microsoft MVP

Google has apparently advised Jon Skeet, C# guru extraordinare, that renewing his MVP status wouldn’t be a good idea. This strikes me as silly for Google to do assuming there is no underlying legal reason for the move. As Google continues to get bigger, its cute little slogans and supposedly fantastic place to work will become less and less interesting. Advising someone with the stature of Jon Skeet that he shouldn’t be an MVP doesn’t exactly jibe with that whole Don’t Be Evil thing.