Functional Programming Koans, in OCaml

(with answers)

[With a bow in the direction of Danny Hillis]

This page is in the spirit of having a little fun while pointing out some of the important concepts of Functional Programming. I wrote most of them, but I got a couple ideas from others, who I will certainly credit if asked. However, all blame for mistakes or inappropriateness should go to me. So without further ado ...

FP Koans (in OCaml)

Once the student has mastered the basics of FP, he is ready to study the FP Koans. The study of FP Koans will help one achieve True FP Nature. (By the way, if you do not know what a koan is Zen Buddhism, it is like a small story or statement given by a master to a student, whose subject is usually not literally about Zen, but it is such that when the student grasps the truth of it they will achieve some true understanding of Zen, perhaps even enlightenment.)

You stand before the mountain.

Learning the art of Functional Programming (FP) is like Zen. In the beginning the student of FP sees his task as a large mountain. Those who study it struggle to climb its heights. Those who know it look back and see no mountain.

The Koan of Imperative/Declarative

A student came to Xavier Leroy in his office one day, and commanded "Tell me how to achieve Knowledge of FP!", to which Xavier replied, "Do you want me to be a good teacher or a bad teacher?" The poor student replied, "I do not understand!" So Xavier replied, "Well, you could have me go to the trouble to tell you how to learn, or instead simply tell you what to learn." And the student was enlightened.

The Koan of Currying (A koan about food, that is not about food)

A student came to Jacques Garrigue and said, "I do not understand what currying is good for." Jacques replied, "Tell me your favorite meal and your favorite dessert". The puzzled student replied that he liked okonomiyaki and kanten, but while his favorite restaurant served great okonomiyaki, their kanten always gave him a stomach ache the following morning. So Jacques took the student to eat at a restaurant that served okonomiyaki every bit as good as the student's favorite, then took him across town to a shop that made excellent kanten where the student happily applied the remainder of his appetite. The student was sated, but he was not enlightened ... until the next morning when he woke up and his stomach felt fine.

The Koan of HOF

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.

The Koan of Side Effects

A student of FP came to Daniel de Rauglaudre and asked how to achieve FP Nature. Daniel replied, "to achieve FP Nature, you must learn to program without side effects and other imperative features". So the student went away to learn how to program in this style. He studied very hard so he could rid his programs of references and for-loops. He struggled to only use let bindings and let rec loops. One day, in order to find inspiration, he was studying the code of the Masters, and in reading the source of Camlp4, he saw a number of uses of references and for-loops! He rushed back to Daniel de Rauglaudre and exclaimed, "How can you tell me not to use imperative features when you use them yourself!" Daniel measured the student carefully in his gaze and replied, "But I already know how to program without side-effects." At that moment the student was enlightened.

The Koan of Lazy Evaluation

Michel Mauny was giving a guest lecture to new computer science students. After the students were seated, Michel pronounced "Today's lecture will be about Lazy Evaluation", he paused for a moment then concluded with, "Are there any questions?"

The Koan of Static Type Safety

A disciple of another sect invited Markus Mottl to the beach. As he is driving his car along, he tells Markus: "Dynamic Typing makes me more productive! I can write my program faster if I do not have to worry about the compiler complaining about types". Since it is a long trip, the disciple of Dynamic Typing decides to make a stop for petrol. He enters the station to pay for the fuel and purchase a slurpee, while Markus attends to the refueling. Markus notes that the petrol pump is in use, so he starts pumping diesel fuel into the disciple's car. The disciple cries out, "Hey! What are you doing, this is a petrol car!". Markus replies, "Well, I wanted to get to the beach faster".

On the other side you look back. There is no mountain.

You have spent months now studying OCaml and one day you wake up and realize that now your code compiles without error more often, runs without error more often, and is shorter and easier to read than you could ever have hoped! Most of all, you realize that programming in OCaml is fun. You have achieved FP Nature.

If you have gotten this far, you may wonder where the answers are! Well, they are here. But you should not read them until you have already read and understood the above koans, it just wouldn't be the same.

  • Last modified: Monday, 03-Feb-2003 07:22:26 CST
  • Copyright © 2002 by Doug Bagley [Email]
  • [Home]