Archive for August, 2008

Project Euler up through Problem 78 in Haskell

For the past couple weeks I’ve been practicing Haskell by working through Project Euler’s problems. I just finished problem 78, which asks you to compute the number of ways to partition a collection of n coins, where n is large. In a way, I feel like I was forced to cheat on this one because I couldn’t do it without looking up the pentagonal number theorem. I hope that future Euler problems do not all degrade into a Google search, because although I learned some math trivia, it’s not a fun way to solve a problem.

In addition to brushing up on number theory, the puzzles have taught me a lot about programming in Haskell. My biggest takeaway is how to memoize. Typically this involves using a data structure such as a Map, constructed from a list of calls to a function that refers back to the data structure. For example:

How many routes are there through a 2020 grid?

> import Data.List
> import qualified Data.Map as Map
> import Data.Map (Map, (!))

> mbound = 1000

> routes = Map.fromList [ ((x,y), route (x,y)) | x <- [0..mbound], y <- [0..mbound] ]

> route (0, 0) = 0
> route (0, b) = 1
> route (a, b) | a > b     = route (b, a)
>              | otherwise = (routes ! ((a - 1), b)) + (routes ! (a, (b - 1)))

> main = do
>  print $ route (20,20)

Notice how ‘route’ refers to the ‘routes’ Map, which in turn refers to ‘route’. This works because of the Haskell’s lazy functional evaluation. How cool is that?

Another lesson is the frequent syntactic idiom of multiple function composition. For example, to create a sorted list of unique items from a list, you can write:

> mynub = map head .
>         group .
>         sort .
>         $ [1,2,5,3,3,1,3,2,4,5,2]

Notice how clean it looks to put each function on its own line, followed by (.). Then you always have to put a dollar sign before the final argument. This is surely obvious to Haskell pros, but it took me a bit to figure this out.

Haskell continues to delight me in its terse elegance. It’s laughable how short nearly every solution is. Now I’m on to Problem 79.

Leave a Comment

Election results

Last Tuesday night my girlfriend’s brother won the democratic primary of a U.S. Congressional race. Since it’s a very strong democratic district, the odds are favorable that he’ll be elected to the U.S. Congress in a few months. In my efforts to keep this blog somewhat anonymous, I won’t give more details on the race.

For the past two weekends I put my shoulder to the wheel and tried to turn out some votes.  I helped my girlfriend go door to door and also made a couple hundred phone calls.  Besides the elation of the victorious election night, I took away two major lessons from the experience.

First, it was inspiring to see how hard a candidate and his supporters must work in order to win.  I only volunteered for two weekends, but the campaign ran at full speed for 15 months.  Despite all the hyped political blabber that you see on the TV, the goal of a campaign is to connect with the voters.  Large media blasts certainly help, but nothing trumps a personal conversation.  The catch is that it’s hard to meet people.  You could spend an entire day walking just one neighborhood.  Since most people aren’t home, and only a fraction of the rest are potential voters, you finish the day wondering if you’ve even accomplished anything.

Second, there’s a lot of potential for improving the role of technology in campaigns.  When we were canvasing, I wished that we had Fedex’s delivery software to plan our route.  I felt like a novice in the super market, bouncing sporadically from aisle to aisle.  When we were making phone calls, I wished that I had a slick computer interface to guide me through the call.  Each person call has subtle features that are easy to overlook.  For example, some people have been called before, some have donated money or time, some are voting absentee, and some share a household with other voters.  In addition, it’s important to impart personalized information, such as the voter’s polling location.

I’ve been thinking about how to create a good user interface for phone banking.  I’m picturing an interface  with a personalized outline (not a script), coupled with a grid of colored boxes containing common questions (where’s my polling location?).  You drill down on any question to reveal both an answer and additional questions on that topic.  There’s also a button to pop back up to the top level.  I wonder if anything like this already exists.

Leave a Comment

Follow

Get every new post delivered to your Inbox.