pozorvlak (pozorvlak) wrote,
pozorvlak
pozorvlak

Functional Koans: a dynamic programmer responds

Sadly, Doug Bagley's Functional Programming Koans seem to have disappeared from the web. Googling will get you some, but not all. But here's my response to some of them anyway.

[If you're unaware of the connection between hackerdom and Zen Buddhism, you should probably go and read the AI koans, and then the Unix koans of Master Foo, though they're not as good. And if anyone can find a complete archive of the Tales of Zen Master Greg I will love you forever. Oh, and here's a nice koan I stumbled across while writing this.]

[And then you should read The Gateless Gate of Mumon, and blow your mind apart.]

The koan of Higher Order Functions

Bagley's original goes:
A disciple who was a recent convert from another sect felt troubled by the teachings of his former master, who taught the dogma that only referentially transparent languages have Functional Nature. So he asks his new master, Pierre Weis: "Does OCaml have Functional Nature?", to which wise master replied: "HOF!" On hearing the mystical incantation, the new convert was enlightened.

But a few days later, the convert returned to the Patriarch Weis, and said, "Master, Perl has higher-order functions. Mark-Jason Dominus has written a book about how to use them well. Does even Perl then have the Functional Nature?"

The master was angered, and he cried "Perl no more has the functional nature than it has the object-oriented nature! Now HOP it!"

At that moment, the student was truly enlightened.

Foomon's commentary: In Caml, as in Perl, There's More Than One Way To Do It.

[NB no similarity to real Caml Patriarchs living or dead is implied]

The koan of Static Typing

I can't find Bagley's original on the web, but here's what I remember:
A dynamic programmer and a static programmer went for a drive one day. Finding that their supply of fuel was low, they stopped at a petrol station, and the dynamic programmer tried to fill up the car. However, he found that he couldn't get the hose to fit into the fuel tank. "Help me fuel the car!" he said to his friend. "But look," said the static programmer, "the pump supplies diesel, and the car only takes petrol. The hose is the wrong size to prevent you from making a terrible mistake".
Here's my reply:
Some time later, the dynamic programmer acquired a new car, and took his friend for a drive in the mountains. Once again, they ran low on fuel, and stopped to fill it up. "You fill up the car while I go to the lavvies" suggested the dynamic programmer. So the static programmer unscrewed the fuel cap, but then realised that all the pumps contained petrol, and he knew his friend had a diesel car. "Well, they're both hydrocarbons," he reasoned, "it can't be that hard to convert one into the other..."

Some minutes later (there had been a long queue) the dynamic programmer returned, to find his car lost in a sprawling mass of tubes, fractionating columns, reaction vessels, and Buddha knew what else.

"Dude, what the hell is this? And what have you done with my car?"
"Oh, I'm just filling up the tank, but they didn't have any diesel. Don't worry, the car's in there somewhere..." The static programmer gestured vaguely.
"Dude, there's no need. It's a dynamically-typed car. Anything you put into it will be alchemically converted to petrol on the way to the fuel tank."
"Really? That sounds a bit risky!"
"Well, there was one time when the radiator was empty and it converted it to distilled water, which was pretty confusing. But then I read the manual, and now I know what it does for me. Pretty stupid - what kind of driver doesn't read the documentation for his vehicle?"
"Doesn't that slow your car down?"
"Well, a bit, but it's got to be easier than carting a small petrochemical refinery about in your suitcase and becoming an expert in alkane synthesis in case you ever run out of diesel."

They set off again on their journey, arguing good-naturedly. Unfortunately, neither attained enlightenment: on the way out of the petrol station they drove through a deep puddle, but were so deep in their argument that they failed to test the brakes afterwards. Later, when they had to brake abruptly on a mountain switchback, the brakes failed. They went hurtling three thousand feet down a cliff, still arguing about the merits of their favourite type systems. Their bodies were never found.
Foomon's commentary: The static programmer was an idiot. The dynamic programmer was an idiot. Everyone is an idiot. Maybe even I, Foomon, am an idiot, though I think it is unlikely. You, reader, are especially idiotic. That is why you should always test your code, no matter how shiny your type system is. And arguing about type systems, though more fun than many things, is unlikely to help much with real-world problems.
Tags: caml, computers, perl, zen
Subscribe

  • New Year's Resolutions 2018

    Happy New Year! This is embarrassing, but I want to break the habit of picking my nose. Lead E1. Same rules as last year. Take 1000 practice…

  • New Year's Resolutions

    Happy New Year! You're all no doubt itching to learn how I got on with my 2016 New Year's Resolutions. [Content note: weight loss/gain] Make a…

  • Lessons learned on my first Alpine trip

    !markdown I got back from my first climbing trip to the Alps a bit over a month ago. My friend Andy and I spent two-and-a-bit weeks in the Écrins…

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 10 comments

  • New Year's Resolutions 2018

    Happy New Year! This is embarrassing, but I want to break the habit of picking my nose. Lead E1. Same rules as last year. Take 1000 practice…

  • New Year's Resolutions

    Happy New Year! You're all no doubt itching to learn how I got on with my 2016 New Year's Resolutions. [Content note: weight loss/gain] Make a…

  • Lessons learned on my first Alpine trip

    !markdown I got back from my first climbing trip to the Alps a bit over a month ago. My friend Andy and I spent two-and-a-bit weeks in the Écrins…