Saturday, October 1, 2011

@ConalElliott re: What resources & practices (teaching Haskell)

Conal Elliott asks: What resources & practices would you recommend for helping a group of Java & C++ programmers learn to work in Haskell? I saw the question first on Twitter, but my compression skills were not up to the task of answering there.

I have two recommendations: teach them the simplest definitions of the fundamentals; read programs with them, out loud, like children's books, skipping nothing.

These steps are often passed hurriedly, by teachers so steeped in a subject they don't realize how different their worlds are, by students eager to build things as quickly in a new programming language as they do in the old. Most likely, the students will assume that they just need to learn new syntax to be off and running. Indeed, I am sure they are capable, self-motivated software professionals and that they would succeed in learning Haskell, just without the deeper understanding that can make it such a joy.

As a test, ask your students to write down the definition of 'type'. I'll bet that their answers will be longer than 'set' and include some mention of bytes, words and big-endian. We were all trained to be mechanics instead of drivers, because all we had were go-carts.

C and its children will have left clutter in their minds. Types will be obstacles, efficiency a drug, correctness assured by machismo. They will gloss over code without really reading it, trying to fit it into an existing model. Saying things out loud helps slow things down, forces them to make connections between symbols, words and definitions so fundamental they are rarely written down. (I backtracked through Paul Hudak's book for hours trying to find out how to say '::'; newcomers ask pretty regularly in #haskell.)

Make sure they know all the places that patterns can appear in code and how they work. Make sure they know that data constructors are functions. Have them verify that with :type in GHCi. And that type constructors are functions, verifying with :kind. They should be familiar with the syntax and semantics of higher-order types before they meet IO, so that they understand it is not magic.

Lists, recursion, functional algorithms, none of these will be challenges, but constructs they have not seen before will give them much more trouble if they cannot deconstruct them to primitives. I cannot imagine Conal omitting such fundamental semantics from his lessons, but I can easily imagine both teacher and students, excited to build new stuff, skimping on the exercises necessary to flush out the old foundations and cement the new.

Sunday, September 11, 2011

Two Degrees from 9/11

On the morning of September 11, 2001, I didn’t board a bus to New York City, and in not doing so, buffered myself from a world of hurt.

I wasn’t clever or prescient, just lucky. I was at the bus stop, I’d seen the bus and flagged it down. But just then, two women jogging by stopped and asked if I had heard what happened. I wanted to keep eye contact with the driver, but the anxiety in their voices made me turn and listen. I glanced back to the driver as he slowed, saw him looking for confirmation, but I didn’t signal him again and he drove on.

I went back home, turned on the TV and soon saw the first tower fall. Had those ladies not happened by, I might well have seen that tower fall from the bus instead.

One friend was not so lucky. He was at jury duty that morning, just ten blocks away from the towers. He saw both towers fall as he walked back to his office in SoHo, breathing the first fumes of the fires, crying uncontrollably at the insanity. Days later, he discovered that a good friend and colleague had been high up in a tower, unable to evacuate. That same victim was actually the boss of another friend of mine, who then had to step up and fill that position.

Another friend was a SCUBA instructor who had trained dozens of NY firefighters to dive. Over a dozen of his students died in the collapse.

Earlier that year, I had attended the wedding of a diver I knew, held at Windows on the World, the restaurant at the top of the north tower. He and his new bride were devastated that so many of the restaurant staff they had worked so closely with to plan the event were lost.

It seemed that everyone lost someone, except for me. My connections were always indirect, like some tragic form of the Kevin Bacon game where I was always degree two. I do not know how much that separation lessened the blow, but it was significant.

On Friday, I went to the NJ side of the Hudson River near the Lincoln Tunnel to see the damage for myself. Though still several miles away, the plume of smoke was trivial to spot, and the lack of that distinctive skyline was like a splinter in my mind.

Still, I did not find myself overwhelmed by anger and grief, like those more closely linked. I looked at New York City, not the way you look when you’ve grown up in a place, but with the eyes of a tourist. The New York metropolitan area is huge. Standing on the palisades, I could see a dense urban landscape, seemingly extending to infinity in all directions.

Suddenly, all I could think of was how little physical damage the attacks had done. Perspective has that effect. The WTC towers were so huge, their presence swamped the mind, especially as one got closer. Just standing at the base and looking up, it felt like they were arching over you instead of going straight up. Their destruction was equally overwhelming. But from where I stood on the palisades, with two degrees of separation in my pocket, I could focus on the thousands of huge buildings, housing millions of people for miles around and see that they were all fine.

I’m not saying the impact wasn’t real, even for someone of degree two. Business dried up and I took advantage of a job offer in San Diego, where I’ve been ever since. All my friends mentioned above stayed and went through some hard times. I sometimes wonder, if I had caught that bus, would I have been degree one, and what might have been different as a result.

Sunday, December 28, 2008

Fish in Monk's Clothing

"Merry Christmas! Could you come to the kitchen and help with the fish?" It wasn't exactly unexpected, but it was still a nice little challenge, like Top Chef the home edition. Unfamiliar kitchen, lots of competition for space, no specific instructions and the opportunity to make or break someone's Christmas dinner. On such occasions, it's good to have a little bit of wisdom from Julia Child in your back pocket.

My travel plans to Illinois had been delayed for two days by snow and ice, so I ended up flying on Christmas day, getting to my brother's house about an hour and a half before guests were showing up for dinner. Everything on the menu was traditional in that household except for the fish, which my brother doesn't eat, so I got volunteered to cook that.

Again, I did have some warning. Several days before, my brother had mentioned on the phone that some guests had asked for fish instead of tenderloin, and that he had bought some frozen tilapia filets. All he needed was a recipe. I said that for simple white fish, I used to like the method that Julia Child had gotten from some monks in the south of France, an episode entitled ``Fish in Monk's Clothing,'' where the fish is baked covered in lots of aromatic vegetables. "Search on the internet, I'm sure you'll find a recipe," I said.

Well, he had searched and had found four recipes for tilapia, but none were Julia's. I didn't like the look of them and although I hadn't made this dish in years, I preferred to find my own way again. Fortunately, the dish turned out well, but the down side was that I had no recipe to give to the people who asked for one, only a bunch of vague constraints.

The essential thing is that the mix of vegetables should taste good, but be reasonably mild so as not to overpower the fish, and should still have a lot of moisture in it. You need enough to cover the fish reasonably well. The vegetables drip flavor into the fish while at the same time protecting it from losing moisture in the direct heat of the oven.

(For the Haskell readers, think of the following as a sort of QuickCheck test suite for the actual method.)
  1. Chop and saute a bunch of aromatic vegetables, season with salt and pepper, thyme or other mild herbs, and reduce with some white wine. The vegetables should be soft, but still very moist and it's good to have some liquid remaining.
  2. TASTE the vegetables. If they don't taste good, fiddle with the seasoning until they do.
  3. Season the filets with salt and pepper, then place on an oiled baking pan. If the tails of the filets are much thinner than the main part of the body, overlap them so that the fish is roughly the same thickness all over.
  4. Layer the vegetables over the fish along with any remaining liquid.
  5. Bake gently until the fish is just opaque and flakes easily. I think we did 350F for about 15 minutes, but this will vary with the amount and size. You could also microwave it for 5-10 minutes, covered, if you use a glass or porcelain casserole dish.
As for which vegetables, that varies based on what I have on hand, but start by sweating an onion in olive oil and butter, then adding celery and a few chopped cloves of garlic. It is fine to brown the onion a bit, but not the garlic. This time we added some chopped mushrooms and parsley. I have added carrots in the past, but that's about as strong as I would go. Julia Child used a head of iceberg lettuce chopped up, but I've never had the nerve to try that. Spinach or swiss chard make a nice choice, but kale, mustard greens and cabbage are too strong. Fennel is a nice addition if you like anise flavor.

Any simple white fish works here, such as flounder, sole, tilapia, catfish, etc. I think I've done it successfully with bluefish, but I would avoid salmon.

How close this is to what Julia Child did, I really don't remember, because I only saw the show once long ago, but I'm pretty sure I've got the essence right. It's a simple way to marry fish with whatever vegetables are available. In any case, it was good enough for Christmas dinner.

Sunday, December 14, 2008

The Pattern of Walking

These images may look like Christmas lights, but they were created simply by holding a camera on long exposure while walking on the beach at dusk. If you just wave the camera around, the patterns are uninteresting, because they have little cohesion, but just walking straight created unexpectedly interesting results.

They remind me of high school physics, when we learned about cycloids by attaching a light to a bicycle tire. The walking images are more regular than I expected, but also far more convoluted than a cycloid. Just as the hidden motion of the tire was revealed in that experiment, these images must show something fundamental about walking. I'm just not sure what.

Monday, November 17, 2008

Monday, November 10, 2008

Functional Programming and the Universe

In Lee Smolin's TED talk How science is like democracy, he describes how our understanding of space and time has evolved. He describes three stages of cosmology:
  1. Aristotelian, which is hierarchical and all properties are defined with respect to that hierarchy;
  2. Newtonian, where properties are all defined with respect to an eternal absolute background of space and time;
  3. Quantum Theory or Relational Universe, where the universe is nothing but an ever evolving network of relationships, and all properties are about relations between subsystems.
Two quotes from the talk that jumped out at me:
According to Newton, space is just the way that god knows where everything is.
And this one, regarding the modern relational model of the universe:
Space is just one aspect, so there's no meaning to say absolutely where something is, there's only where it is relative to everything else that is, so we call it a relational universe.
In other words, even the physicists don't use an object-oriented model to describe objects any more.

Friday, October 31, 2008

Halloween Slideshow

As a public service, I've made a Halloween slideshow out of photos I've taken over the last year. Comprising over a hundred scary, ghoulish and horrifying images, you can set it playing on a spare monitor and be frightened all day long.