Infinite Recursion

September 2, 2009

A few days ago I posted an article on Tail Call Optimization. One really quick way to determine if any language/VM/interpreter performs Tail Call Optimization (TCO) is to write a function that calls itself, in other words, creating an infinitely recursive function. If the function just runs forever and doesn't return, the interpreter is doing TCO, otherwise you will get some sort of stack overflow error. So I decided to test a variety of languages to see what happens when you write an infinitely recursive function. First up is ruby:

def forever


It was no surprise to me that running this results in this error:

run.rb:2:in `forever': stack level too deep (SystemStackError)
    from run.rb:2:in `forever'
    from run.rb:5

Ruby doesn't have TCO, I knew that. Next up, Python:

def forever(): forever()


This has a similar result, but the stack track is a little more revealing:

Traceback (most recent call last):
  File "", line 3, in <module>
  File "", line 1, in forever
    def forever(): forever()
  File "", line 1, in forever
    def forever(): forever()
  File "", line 1, in forever
    def forever(): forever()
  File "", line 1, in forever
    def forever(): forever()
RuntimeError: maximum recursion depth exceeded

This shows pretty clearly what is going on, the stack frames are pilling up and eventually it gets to the point where the Python interpreter says enough is enough. Interestingly enough, this mailing list thread shows that it's completely feasible to add TCO to Python and Guido just doesn't want to add it.

JavaScript is no surprise either, but we can write our infinitely recursive function with an interesting little twist:

(function(){ arguments.callee() })()

Yes that's right, it's an anonymous recursive function! Running this with SpiderMonkey results in InternalError: too much recursion. So how about Java:

class Run {
  public static void main(String[] args) {
    Run run = new Run();

  public void forever() {

The stack trace for this one looks a bit like the Python one, we see a stack frame for each iteration:

Exception in thread "main" java.lang.StackOverflowError
    at Run.forever(
    at Run.forever(
    at Run.forever(
    at Run.forever(

So that means no TCO on the JVM. So Scala doesn't have TCO, right?

def forever : Unit = forever


Wrong. This one runs forever. Scala does TCO with some bytecode tricks which Nick Wiedenbrueck explains really well in this blog post.

Clojure is a functional Lisp saddled with the problem of no-TCO on the JVM, but it gets around it in a slightly different way than Scala. If you write a tail recursive function like this:

(defn forever [] (forever))

You will get a java.lang.StackOverflowError just as you do in Java. Instead, Clojure provides a language level feature to do recursion:

(defn forever [] (recur))

Calling recur in the function makes it call the function again. recur also checks that it is in the tail recursive position, which ends up being an interesting feature because you have to explicitly say "I expect this to do TCO". In other languages that do it transparently, you might think TCO is happening, but it might not be and you won't find out until you get a stack overflow error. Also, this construct allows us to have recursive anonymous functions:

((fn [] (recur)))

Erlang, being a function language, has TCO as well:



forever() ->

Now one thing all of these functional languages that have TCO; Scala, Clojure and Erlang, all have in common is that while the infinitely recursive function runs, it essentially does nothing, but it will peg your CPU utilization to nearly 100%. This next language, Haskell, being the mother of all functional langauges, of course has TCO. Here's the function:

forever = forever

Yes, that's a function. And if you call it with forever, it just sits there and runs forever. But here's the crazy thing, it uses no CPU. My guess is that it has to do with lazy evaluation, but I'm not sure. Any ideas?

Posted in Technology | Tags TCO, Javascript, Scala, Haskell, FunctionalProgramming, Python, TailCallOptimization, Ruby, Java, Erlang, Clojure | 19 Comments

Tail Call Optimization

August 30, 2009

Tail-Call Optimization (a.k.a Tail Recursion) is a technique often used in functional programming languages that promotes the use of recursion rather than imperative loops to perform iterative calculations. When I say imperative loops, I mean using a local variable that you change the value of as you calculate each step of the iteration. A generic term for this kind of variable is an accumulator, because it accumulates the new value at each step of the iteration.

Let's say you are hoping to become the next mega millionare and you want to know your odds of winning. Most lotteries consist of something like pick 5 numbers between 1 and 50. Each number can only come up once in the drawing and if you get all 5 numbers right, you win. The order isn't significant, you just need to get the 5 numbers.

Here's a JavaScript function written in an imperative style to get your odds:

function odds(n, p) {
  var acc = 1
  for(var i = 0; i < n; i++) {
    acc *= (n - i) / (p - i)
  return acc

The parameters are n, the number of numbers you have to pick and p is the upper limit of all possible numbers to choose from. So the odds for the "pick 5 of 50" are 4.71974173573222e-7. That's expressed in decimal in scientific notation. To get the more traditional looking "1 in whatever" number, you simply take the inverse, 1/4.71974173573222e-7, which is 1 in 2,118,760.

The mega millions actually has a twist to it with the powerball. You pick 5 numbers of of 56 and then 1 number out of 46. A number that comes up in the first 5 can come up as the powerball. So that means the mega millions odds are 1/(odds(5,56) * odds(1,46)), which is 1 in 175,711,536.

One problem with our function is that it uses a mutable local variable and mutable local variables are evil. Some functional languages, such as Clojure, Haskell and Erlang, don't even allow you to use mutable local variables. So how can we do it without the accumulator? The answer is recursion:

function odds(n, p) {
  if(n == 0) {
    return 1
  } else {
    return (n / p) * odds(n - 1, p - 1)

This is a pretty elegant implementation of the function because it mirrors how you might think of the problem. This still works great but it breaks down if we try to calculate the odds of much larger numbers. For example, if you try to calculate the odds of picking 10,000 numbers out of 100,000 numbers with odds(10000, 100000), you will some kind of stack overflow / too much recursion error, depending on your interpreter.

The reason for this becomes clear when you walk through how the interpreter calculates the value. When you say odds(5, 56), the return value is (5/56) * odds(4, 55). The interpreter cannot evaluate this expression until odds(4, 55) is calculated. So the series of expressions that get evaluated are:

odds(5, 56)
(5/56) * odds(4, 55)
(5/56) * (4/55) * odds(3, 54)
(5/56) * (4/55) * (3/54) * odds(2, 53)
(5/56) * (4/55) * (3/54) * (2/53) * odds(1, 52)
(5/56) * (4/55) * (3/54) * (2/53) * (1/52) * odds(0, 51)
(5/56) * (4/55) * (3/54) * (2/53) * (1/52) * 1

As you can see, the lines get wider as the recursive calls build up. This is representative of how the interpreter works. As each recursive call is added, memory is temporarily used up. Once there are no more recursive calls, it can start evaluating the expressions and freeing up the memory. This works fine, but if you are performing the calculation on a large number that generates more recursive calls than the interpreter can hold in memory, then boom goes the dynamite. Most interpreters push each recursive call onto a stack, so when the stack runs out of memory, you get a Stack Overflow error.

So how can we possibly do calculations like this recursively? The answer to that is tail-call optimization. In order to do this, we are going to have to bring back the accumulator, but in this case, it's not going to be a mutable local variable, instead it will be a parameter to the function. Since we don't want callers of our function to have to pass in the initial value of the accumulator, we will define two functions, one "public" and the other "private":

  var odds1 = function(n, p, acc) {
    if(n == 0) {
      return acc
    } else {
      return odds1(n - 1, p - 1, (n / p) * acc)

  odds = function(n, p) {
    return odds1(n, p, 1)

JavaScript doesn't really have a public/private mechanism, but we can use closures to accomplish the same thing. First this is an anonymous function that we create and then call immediately after we create it. This just gives us a local scope. If we didn't have this outer wrapping function, all variables would be global. Then we define a function and assign it to a local variable called odds1. Next we define a function and assign it to a global variable odds. The function odds is "closed over" the variable odds1, which means that it can reference that variable, even when the odds function is called outside the context it is defined in, even though the odds1 variable will not be directly available from that context. To test this out, simply try to print the value of odds and then odds1 from outside of this outer function. The interpreter will say odds is a function but it will say odds1 is undefined, which means odds1 is effectively private.

So when someone calls our function odds, it calls odds1 with an initial value of 1 for the accumulator and then returns that value. Since JavaScript does not have tail-call optimization, that is what actually happens. odds waits for the value of odds1 to be calculated and then returns that value once it gets one. If you look at odds1, it does the same thing, returning the value from a recursive call to itself. And since JavaScript does not have tail-call optimization, this still results in some sort of stack overflow with large enough values for n.

What a tail-call optimized language does is realize that the statement return odds1(n, p, 1) is saying call the function odds1 with the arguments n, p and 1 and then return value. What the language can do is have the odds function return immediately and tell the caller to call odds1 with the arguments n, p and 1 in order to get the value. The important distinction between these two is that the call to odds is eliminated from the call stack before odds1 is called.

The way I think of it is tail-call optimization almost like an HTTP redirect. The call to odds is simply redirected to odds1. Once odds has issued a redirect, it's out of the equation. Without tail-call optimization, the call to odds has to make a call to odds1. While the call it odds1 is being processed, both odds and the original caller are waiting for the response. If the chain of calls becomes to long, the call stack overflows.

This same idea can be applied to the recursive calls within odds1 itself. When odds(5, 56) is called, it will in turn call odds1(5, 56, 1). In odds1, the last thing it will do is call itself recursively, but this time the parameters will be odds1(4, 55, (5/56)). A function that returns a recursive call to itself as the last thing it does is said to be tail-recursive. Our original recursive odds function was not tail-recursive, because it needed to multiple the value of the recursive call by a value after the recursive call returned.

Since odds1 is tail-recursive, the call to odds1(5, 56, 1) can just say call odds1(4, 55, (5/56)) instead and take itself off the call stack. The series of calls looks like this:

odds(5, 56))
odds1(5, 56, 1)
odds1(4, 55, 5/56)
odds1(3, 54, 1/154)
odds1(2, 53, 1/2772)
odds1(1, 52, 1/73458)
odds1(0, 51, 1/3819816)

The value of the accumulator in each of these calls is the result of the multiplication of the previous value and the current n divided by the current p. As you can see here, the width of the expression doesn't increase for each iteration (well, except a little bit for the width of the accumulator value). If the interpreter actually evaluates the expressions this way there is no way to cause a stack overflow error. You can evaluate any of these expressions independently from the others, assuming you make odds1 public.

To see this in action, you will have to use a language with an interpreter that does tail-call optimization. Erlang is one such language. First let's write a non-tail-recursive implementation of the odds function:



odds(0, _) -> 1;
odds(N, P) -> (N / P) * odds(N - 1, P - 1).

Now we can see that our function works using the Erlang interactive shell:

$ erlc lotto.erl
$ erl
Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2  (abort with ^G)
1> lotto:odds(5,56).
2> 1/(lotto:odds(5,56) * lotto:odds(1,46)).

So it turns out that you have to work really hard to get the Erlang VM to blow the stack on a non-tail-recursive call, but I managed to get an error eheap_alloc: Cannot allocate 1140328500 bytes of memory (of type "old_heap"). by trying to evaluate lotto:odds(100000000, 1000000000).. Let's re-write the function to be tail-recursive:



odds(N, P) -> odds(N, P, 1).

odds(0, _, Acc) -> Acc;
odds(N, P, Acc) -> odds(N - 1, P - 1, (N / P) * Acc).

You can see that Erlang gives us a couple of language features to make this a little cleaner than the JavaScript function. First, functions with different arities are completely different functions. Arity is the number of arguments to a function. So here we have two functions, odds/2 and odds/3.

Also, pattern matching is used to distinguish the two cases. The odds/3 function has two cases, one where N is 0 and the other to handle all other cases.

Lastly, the export directive at the top says that we only want the odds/2 function to be public, so the odds/3 function is private to this module.

With this definition of the odds function, if we try to evaluate lotto:odds(100000000, 1000000000)., it will complete, although it will take some time. The original recursive odds function takes O(n) time and O(n) space, which means the amount of time and the amount of space it takes to evaluate the function increases linearly for larger values of n. Due to tail-call optimization, the function still takes O(n) time, but takes O(1) space, which means the amount of time it takes to evaluate the function still increases linearly with larger values for n increases, but the function always operates in a constant amount of space, regardless of the size of n.

So to summarize, tail-call optimization is a technique that is performed by the compiler/interpreter, usually transparently, to make tail-recursive functions operate in constant space. This is a necessary optimization in order to be able to use recursive functions to perform iterative calculations as opposed to imperative functions that use mutable local variables.

Posted in Technology | Tags TCO, Javascript, Recursion, Haskell, FunctionalProgramming, TailCallOptimization, Erlang, Clojure | 14 Comments

Currying and Partial Function Application

August 21, 2009

Two related but different concepts that you will run into when doing functional programming are currying and partial function application. People often don't understand the different between the two, I didn't myself until recently, but I think I've got a handle on in now. Currying and partial function application are two different things, but they go together like peanut butter and jelly.

First, let's start with currying. Currying is the process of transforming a function that takes n arguments into a function that takes n-1 arguments and returns a function. What the hell does that mean? Let's look at an example in everyone's favorite functional programming language, JavaScript.

Here's a simple function that returns true if the first argument is greater than the second argument:

function gt(x,y) {
  return x > y

Pretty basic. If we call gt(3,2), that will obviously return true. If we call gt(2) though, we get false, because it ends up evaluating the expression 2 > undefined, which is false. So our gt function is pretty useless unless you pass it two arguments.

Now what if we write it like this:

function gt(x,y) {
  if(arguments.length > 1) {
    return x > y
  } else {
    return function(z) {
      return z > x

Now there is a lot of crazy lambda calculus going on here. If you pass two arguments to gt, it works as before, returns true if x is greater than y. But if you pass just one argument, this function creates a function that takes one argument and returns true if the argument is greater than x.

This is an example of a closure. We say the function that is returned "closes over" x, which just means that the function remembers what the value of x was at the time it was created, so when you call it later, it still uses that value.

So if we call gt(2), the result we get back is a function that returns true if the argument to it is greater than 2. This is called partial function application.

In functional programming, the correct term is actually not "call a function", but "apply a function to a set arguments". That's why in JavaScript when you want call a function with arguments that you have in an array, you use the apply function, like this:


The first argument to apply is the object that the reference this should point to during the function call, and in this case we just pass null because we don't care about the this reference. The next argument is the set of arguments, 3 and 2, which become the first and second arguments to the call to gt.

So now the term partial function application makes sense, because we are applying a partial list of arguments to the function. Now it should be clear why currying and partial function application go together hand-in-hand. When you partially apply a function that is curried, you get something useful back. Partially applying a function that is not curried isn't usually useful, as we saw in the first example, where calling gt(2) resulted in evaluating the expression 2 > undefined, which is just silly.

So in the case of our curried version of gt, is the result of partially applying actually useful? Sure it is, when we combined it with other functional concepts. All good functional programming language have some kind of filter function, and JavaScript (well, JavaScript 1.6. Better late than never) is no exception.

Filter is a higher-order function (whew, we are busting out all the fancy functional programming words today, aren't we!). It is a higher-order function because it takes another function as it's argument. What filter does is allow you to take an array of elements and produce another array with only the elements than match some condition. The function that defines what the condition is is typically a predicate function.

So let's filter an array of number an only get the ones greater than 2. First we'll do it with an anonymous function as the predicate function:

  return e > 2

That will result in an array with 3, 4 and 5 in it. That's a little wordy though. Let's use our curried gt as the predicate instead:


Ah, there we go, that's nicer. That's gives us 3, 4 and 5 as well. Of course you can just change the value of the argument to gt to filter for values greater than that number.

Now I should point that technically, our gt function is not in fact partially applied when we pass in less than two arguments, it just makes a new function. If we define gt like this:

function gt(x) {
  return function(y) {
    return y > x

This means that gt always takes two arguments and returns a function that takes one argument. This still works the same in our filter example, [0,1,2,3,4,5].filter(gt(2)) does the same thing. But when you want to just call gt directly with two arguments, that doesn't fly. gt(3,2) now just ignores the second argument and returns a function the same way gt(3) would. If we want to simulate our two argument call, we have to make two function calls, like gt(2)(3), which looks hideous. Not only do we have two sets of parentheses, it is also has to be backwards. To ask is 3 > 2, we has to pass in 2 first in order to the "greater than 2" function, and then pass 3 to that.

Haskell has built-in syntactic sugar to make this look not hideous. Haskell is a statically-typed functional programming language. Being statically-typed means that variables are of a specifc type. JavaScript is the opposite, dynamically-typed. In JavaScript, when you have a variable or a function argument that is named x, x can hold any type of Object, a String, Number, Function, etc. In Haskell, when you define a variable or function argument, that must always be the same type. Haskell is actually pretty clever about it though. If you just say x = 42, it knows, ok, x is an Int. In other, less sophisticated statically-typed languages, you would have to say something like int x = 42.

When you look at the type signature of a function in Haskell, at first, it looks odd. For example, the type signature for our gt function might look like:

gt :: Int -> Int -> Bool

At first you think, what's with the arrows? Wouldn't something like this make more sense:

gt :: (Int, Int) -> Bool

Yeah, that matches what we are thinking. gt is a function that takes two Int values and returns a Bool. Well, even though you might think of it that way, that's not what it does in Haskell. In Haskell, all functions are curried. So our gt function is actually much closer to our last definition of gt in JavaScript, which looks like this:

function gt(x) {
  return function(y) {
    return y > x

If you look at that for a minute, you'll agree that a type signature like this makes sense.

gt :: Int -> Int -> Bool

gt takes one argument and returns a function that takes one argument that returns a Bool. Unlike JavaScript, Haskell's syntax makes calling curried functions clean. So again, in JavaScript, we would have to do this to call our function:


Which is the same as 3 > 2, but in Haskell if we want that, we simply just say 3 > 2. In Haskell, infix operators are actually function calls. 3 > 2 is syntactic sugar for (>) 3 2. Also in Haskell, you don't have to wrap the function call in parentheses to call it. The parentheses in this snippet are just the way of getting a reference to the infix function without calling it. If you could name a function > in JavaScript, the Haskell snippet (>) 3 2, or even 3 > 2, would be the equivalent of >(3, 2) in JavaScript.

So when we want to partially apply this function to get a function that returns true if the value is greater than 2, we just write (> 2). Haskell knows how to "do the right thing" with infix functions like greater than, so we don't have to deal with the problem of the arguments being backwards. So here's how we do the filter, and we can do it by partially applying the built-in greater than function (this is the advantage of > being a function, not an operator) to get the "greater than 2 function" to pass to filter:

filter (> 2) [0..5]

This, of course, results in the list [3,4,5]. So that's a whirlwind tour of a lot of functional programming concepts, but we can sum it all up with this:

A curried function is a function that will return a new function when it is applied to less arguments than it expects and applying a function to less arguments than it expects is called partial function application. So, stated even more succinctly, a curried function is a function that will return a new function when it is partially applied.

Posted in Technology | Tags Javascript, Currying, PartialFunctionApplication, Closures, Haskell, FunctionalProgramming | 4 Comments