February 18, 2018
This site is focused on applying functional programming to practical problems in web development. But in this article, we will be covering something that seems decidedly un-practical: we’re going to use OCaml to…solve a math puzzle!
What on earth could that possibly have to do with web development, functional or otherwise? Well not so much directly…but bear with me, because this problem actually illustrates a lot of interesting concepts from functional programming. And those concepts will end up coming up over and over again in FP and here on funlambda.
But don’t worry — we will also be covering lots of practical problems in modern web development at funlambda. But in the mean time, I’d encourage you to go through this series. Stick around to learn:
So without futher ado, let’s tackle the problem!
Imagine you are a farmer standing on the southeast corner of a perfectly square pen. Directly in front of you, there is a gate at the pen’s northeast corner.
Suddenly without warning, a wild boar breaks into the pen’s southwest corner…and he’s charging directly at you!
You try to flee as fast as you can by running straight for the exit, but as you move the boar continues moving right at you.
If you can run fast enough you will reach the exit before the boar catches up with you and escape unscathed.
But if the boar is fast enough…you will not be so lucky!
If the boar is running the same speed as you are, you will be just fine. Even if it knew to run directly to the exit to cut you off (which it doesn’t), it would have a greater distance to cover than you, so you would still escape. But at some boar speed, you will not be able to make it out before he collides with you!
First let’s normalize the problem to make the math easier to deal with. Instead of worrying about the size of the pen, the speed of the farmer, and what units to use, we will just assume the pen is a 1x1 square, and that the farmer’s speed is 1 (all unit-less). This way, the speed the boar has to run to catch the farmer will be relative to the farmer’s speed, which is the exact answer we’re looking for!
With this simplification, all the action we care about will be happening on the unit square:
Next, let’s try to determe what the path of the farmer will look like. The farmer starts at \(<1,0>\) and reaches \(<1,1>\) at \(t=1\), running at a constant speed. This can be modelled very easily with this equation:
$$f(t) = <1,t>$$
How can we represent this in code? Let’s use an OCaml tuple to represent 2-dimensional vectors:
let farmerPos t = (1., t)
We’re going to be using vectors a lot to solve this problem, so let’s go ahead and create a module for our new vector type. Let’s also add some functions that we’ll need to add, scale, and get the norm (magnitude) of vectors.
Not familiar with modules yet? Read more here: ReasonML: basic modules. This resource is focused on ReasonML, but it applies to OCaml as well, just with slightly different syntax.
Here’s what our new vector module looks like:
(* Vector2.ml *)
type t = float * float
let zero =
(0., 0.)
let add
((x1, y1)) ((x2, y2)) = (x1 +. x2, y1 +. y2)
let scale scalar ((x, y)) =
(scalar *. x, scalar *. y)
let norm ((x, y)) =
sqrt ((x ** 2.) +. (y ** 2.))
Other files in our project will be able to reference these functions like this:
let sum = Vector2.add v1 v2
let scaled = Vector2.scale (2.) v1
It would be nice if we could just use our usual mathematical operators (+,*) with our new vector types. In OCaml, operators (like all functions) cannot be used for different types. However, we can define our own custom operators to work on vectors:
let (%+) a b = add a b
let (%*) t v = scale t v
let (%-) a b = add a (scale (-1.) b)
Note that subtraction is equivalent to adding a vector scaled by -1.
And here’s what our farmer position function looks like plotted. Drag the slider or change the textbox value to see the path for different values of t.
In the next post, we will derive an equation that governs the path of the boar and numerically solve that equation with OCaml.
Written by Brooks Noble, a web developer building complex web apps using functional programming techniques.