In this article, we are going to show how one could write recursive functions in a language where there are no names and no loops.

Escher

With no explanations, we are going to show the magic in action: the recursive implementation of the factorial function without using neither names nor loops.

All the code snippets of this page are live and interactive powered by the klipse plugin:

  1. Live: The code is executed in your browser
  2. Interactive: You can modify the code and it is evaluated as you type

There are versions of the code in: clojure, javascript, EcmaScript6 and ruby.

Feel free to play with the value passed to the function in your preferred language. We are starting with 19:

Clojure

(((fn [f]
   (f f))
  (fn [func]
   (fn [n]
    (if (zero? n)
     1
     (* n ((func func) (dec n)))))))
 19)

Here is a detailed explanation of this code.

Javascript

(function (f){
  return f(f);
})(function (func){
  return (function (n){
    if (n === 0){
      return 1;
    }
    else {
      return (n * func(func)(n - 1));
    }
  })})(19)

Here is a detailed explanation of this code.

EcmaScript 6

It will work only if your browser supports EcmaScript6 arrow functions.

((f => f(f)))
(func => n => (n === 0)? 1 : (n * func(func)(n - 1)))
(19)

Here is a detailed explanation of this code.

Ruby

->(f){
  f[f]
  }[->(func){
    ->(n) { n == 0 ? 1 : n * func[func][n-1]}
    }][19]