Ну и для полноты картины решение на С++. Решение "в лоб", без придумок, с использованием библиотечных замыканий:
#include <boost/bind.hpp>
#include <boost/function/function0.hpp>
#include <iostream>
typedef boost::function0<int> function_t;
int f0(){return 0;}
int f1(){return 1;}
int fn1(){return -1;}
int b(int& k,
function_t const& x1,
function_t const& x2,
function_t const& x3,
function_t const& x4);
int a(int k,
function_t const& x1,
function_t const& x2,
function_t const& x3,
function_t const& x4,
function_t const& x5)
{
return k <= 0 ? x4() + x5() : b(k, x1, x2, x3, x4);
}
int b(int& k,
function_t const& x1,
function_t const& x2,
function_t const& x3,
function_t const& x4)
{
--k;
return a(k,
boost::bind(&b, boost::ref(k), x1, x2, x3, x4),
x1,
x2,
x3,
x4);
}
int main()
{
std::cout << a(10, f1, fn1, fn1, f1, f0) << std::endl;
return 0;
}